This is because t really not a type. This is an instance of type Metatype :
let x = NSString.self
You just want to check t , which is the actual type, but you can use T.Type to determine if t :
// genericised a bit, to make list an argument func contains <S: SequenceType, T> (list: S, type t: T.Type) -> Bool { for element in list { if element is T { return true } } return false } let a = ["a","b","c"] contains(a, type: String.self) // true contains(a, type: NSString.self) // true contains(a, type: Int.self) // false contains(a, type: NSNumber.self) // false let b: [Any] = [1, 2 as NSNumber, "c" as NSString] contains(b, type: String.self) // true contains(b, type: NSString.self) // true contains(b, type: Int.self) // true contains(b, type: NSNumber.self) // true
Remember that t is still defined statically at compile time, and not dynamically. So:
let c: NSObject = 1 as NSNumber contains(a, type: c.dynamicType)
returns true not false because it checks for an NSObject (since the result type of c.dynamicType is NSObject.Type not NSNumber.Type ).
source share