gpt4 book ai didi

ios - 将 ViewController 与 swift 中的类型进行比较

转载 作者:行者123 更新时间:2023-11-28 09:36:18 25 4
gpt4 key购买 nike

extension UIViewController {
func getChildViewController<T>(OfType: T) {
let classType = Mirror(reflecting: self.childViewControllers.first).subjectType

if classType == T.self {
print("there is a match")
}
}
}

这是对 UIViewController 的扩展,当你调用这个函数时,你传递给它一个类型,例如:

ViewController.getChildViewController(OfType: SecondViewController.self)

这将检查 View Controller 的第一个子节点是否为 SecondViewController 类型

但是在 if 语句中我得到了错误:

Binary operator '==' cannot be applied to operands of type 'Any.Type' and 'T'

最佳答案

Swift 3 中的 isKindOf 只是 is 所以你应该使用像这样的东西:

if classType is SecondViewController {
print("there is a match")
}

关于ios - 将 ViewController 与 swift 中的类型进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41671049/

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