gpt4 book ai didi

swift - 有什么方法可以使 segue.destinationViewController 成为?判断 UIViewController 之一

转载 作者:行者123 更新时间:2023-11-30 13:54:55 29 4
gpt4 key购买 nike

我的代码目前是这样的:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let des = segue.destinationViewController as? AURReportViewController {
des.currentType = currentType
}

if let des = segue.destinationViewController as? AURAddBeanViewController {
des.currentType = currentType
}
}

有没有办法同时在 UIViewController 之一中进行 make as?

if let des = segue.destinationViewController as? (AURReportViewController || AURAddBeanViewController) {
des.currentType = currentType
}

最佳答案

不,没有。这两种类型不会相互继承。 Swift 是强类型的。在您的提案中,if let 内, des可以是两种不同类型之一。编译器未设置为处理“在此 block 中此变量可能是 AURAddBeanViewController 或可能是 AURReportViewController(或者可能是...等)”的情况。编译器想要确切地知道每个变量是什么类型。

现在如果每个类都符合共享协议(protocol)HasCurrentType ,编译器可以理解“在这个 block 中,这个变量符合 HasCurrentType”。类型安全规则得到遵守,编译器很高兴。

苹果文档protocol conformance testing在“检查协议(protocol)一致性”部分中,示例名为 HasArea 。所以在你的情况下,if let des = segue.destinationViewController as? HasCurrentType ...

关于swift - 有什么方法可以使 segue.destinationViewController 成为?判断 UIViewController 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749536/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com