Swift比较两个MetaTypes?

时间:2015-01-03 16:43:19

标签: swift

我需要测试一下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 {

}

1 个答案:

答案 0 :(得分:1)

函数重载将是我的第一选择,但要回答这个问题;使用NSStringFromClass或以下内容:

@objc class Raw {

}

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

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

    // return something
}

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