2017-01-16 102 views
0
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) 

这将检查的第一个孩子视图控制器如果类型SecondViewController的

但是在if语句,我得到的错误:

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

我莫名其妙地成功斯威夫特编译这3 – Sweeper

回答

0

就不得不强制转换为:any.Type

let passedType = ofType as? Any.Type 
if classType == passedType { 
    print("there is a match") 
} 
1

当您需要调用泛型的参数名称时,您只需调用泛型。

你试过:

if classType == ofType.self { ... 
+0

啊,没错只是去尝试仍然得到错误:二元运算符“==”不能应用于类型“Any.Type”和“T”的操作数 –

0

斯威夫特3 isKindOf只是is所以你应该使用类似:

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

我认为应该使用=== 苹果迅速的编程语言,p.768