Swift 比较两个元类型?
Swift compare two MetaTypes?
我需要测试我传递给函数的类型是否属于某个 class 或
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 {
}
函数重载是我的首选,但要回答这个问题;使用 NSStringFromClass
或以下内容:
@objc class Raw {
}
func doSomething<T: AnyObject> (type: T.Type) -> T {
type as AnyClass === Raw.self as AnyClass { }
// return something
}
这个解决方案似乎也适用于纯 Swift 类。
我需要测试我传递给函数的类型是否属于某个 class 或
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 {
}
函数重载是我的首选,但要回答这个问题;使用 NSStringFromClass
或以下内容:
@objc class Raw {
}
func doSomething<T: AnyObject> (type: T.Type) -> T {
type as AnyClass === Raw.self as AnyClass { }
// return something
}
这个解决方案似乎也适用于纯 Swift 类。