I'm not sure if your `if` testing works as expected.
func test1(strClass: AnyClass) {
if strClass == NSString.self {
print("true")
}
}
test1(NSString.self) //->true
let nsStr: NSString = "string"
test1(nsStr.dynamicType) //(no output)
You can write an equivalent testing with `switch`, if you implement a matching operator for AnyClass.
func ~= (lhs: AnyClass, rhs: AnyClass) -> Bool {
return lhs == rhs
}
func test2(strClass: AnyClass) {
switch strClass {
case NSString.self:
print(true)
default:
print(false)
}
}
test2(NSString.self) //->true
test2(nsStr.dynamicType) //->false
A little bit different type testing can be written with `switch` as follows:
func test3(strClass: AnyClass) {
switch strClass {
case is NSString.Type:
print(true)
default:
print(false)
}
}
test3(NSString.self) //->true
test3(nsStr.dynamicType) //->true