[英]Swift compare two MetaTypes?
I need to test to see if a Type I'm passing to a function is of a certain class or not 我需要测试以查看我传递给函数的Type是否属于某个类
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 {
}
Function overloading would be my first choice, but to answer the question; 函数重载将是我的首选,但要回答这个问题。 either use
NSStringFromClass
, or the following: 使用
NSStringFromClass
或以下代码:
@objc class Raw {
}
func doSomething<T: AnyObject> (type: T.Type) -> T {
type as AnyClass === Raw.self as AnyClass { }
// return something
}
This solution seems to work with pure Swift classes too. 这个解决方案似乎也适用于纯Swift类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.