我只想将任何 UIViewController
子类转换为 AnyClass
以便从 isKindOfClass
方法进行检查,因为此方法采用 AnyClass
作为参数。
最佳答案
您可以在 swift 中使用 as
或 is
关键字,而不是使用旧的 Objective-C
API。
if let controller = yourViewController as? SomeClass {
// use controller, is already casted to SomeClass
}
if yourViewController is SomeClass {
let controller = yourViewController as! SomeClass
// use controller now
}
添加:
如果内存地址不同,则对象不相等。尝试检查我在答案中提到的 topViewController
类型。
if let topViewController = appDelegateObj.navigationController.topViewController as? YOUR_VIEW_CONTROLLER_CLASS {
// do something
}
关于ios - 如何在 Swift 中将 UIViewController 转换或类型转换为 AnyClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966962/