Dyl*_*lan 0 generics comparison uiviewcontroller ios swift
extension UIViewController {
func getChildViewController<T>(OfType: T) {
let classType = Mirror(reflecting: self.childViewControllers.first).subjectType
if classType == T.self {
print("there is a match")
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是UIViewController的扩展,当您调用此函数时,可以将其传递为一种类型,例如:
ViewController.getChildViewController(OfType: SecondViewController.self)
Run Code Online (Sandbox Code Playgroud)
这将检查视图控制器的第一个孩子是否类型为SecondViewController
但是在if语句中,我得到了错误:
Binary operator '==' cannot be applied to operands of type 'Any.Type' and 'T'
Run Code Online (Sandbox Code Playgroud)
在isKindOf斯威夫特3就是is这样,你应该使用这样的:
if classType is SecondViewController {
print("there is a match")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3451 次 |
| 最近记录: |