我需要测试一下I型传递给某个功能是否属于某个类
func doSomething<T> (type: T.Type) -> T {
// Check to see if type is the same as Raw type?
if type == Raw.self { } // Doesn't work, Compile error
if type is? Raw.self { } // Doesn't work, Compile error
}
@objc class Raw {
}
答案 0 :(得分:1)
函数重载将是我的第一选择,但要回答这个问题;使用NSStringFromClass
或以下内容:
@objc class Raw {
}
func doSomething<T: AnyObject> (type: T.Type) -> T {
type as AnyClass === Raw.self as AnyClass { }
// return something
}
此解决方案似乎也适用于纯Swift类。