繁体   English   中英

Swift比较两个MetaType?

[英]Swift compare two MetaTypes?

我需要测试以查看我传递给函数的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 {

}

函数重载将是我的首选,但要回答这个问题。 使用NSStringFromClass或以下代码:

@objc class Raw {

}

func doSomething<T: AnyObject> (type: T.Type) -> T {

    type as AnyClass === Raw.self as AnyClass { }

    // return something
}

这个解决方案似乎也适用于纯Swift类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM