2017-08-09 68 views

回答

3

您可以使用type(of:)得到object的類型,並將其與AnyClass比較。試試這個..

func checkGeneric(className: AnyClass) { 
    let object = UIViewController() 
    if (type(of: object) == className) { // Use of undeclared type class name 
     print(className) 
    } 
} 
+0

謝謝它的作品:) –

1

您還可以isKinOf

func checkGeneric(className: AnyClass) 
    { 
     print(className) 
     let object = UIViewController() 
     if object.isKind(of: className) { 
      print("yes") 
     } else { 
      print("no") 
     } 

    } 

    checkGeneric(className: UIViewController.self) 
    checkGeneric(className: NSMutableArray.self) 

OUPUT做

UIViewController 
yes 
NSMutableArray 
no 
+0

這也適用我欣賞:) –

0

試試這個

func checkGeneric(className: AnyClass) { 
    let object = ViewController() 
    if (object.isKind(of: className)) { 
     print("Class Name is : \(className)") 
    } 
} 

checkGeneric(className: ViewController.self) 

您可以比較C對象lass使用isKind(),

let viewController = UIViewController() 

viewController.isKind(of: ViewController.self)