Protocol是一个不透明的对象类型。它在生成的标头中定义为:
// All methods of class Protocol are unavailable.
// Use the functions in objc/runtime.h instead.
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
@interface Protocol : NSObject
@end
它不符合MyProtocol,所以is MyProtocol不能工作。而且,尽管 Swift 可以隐式地@objc将协议元类型桥接到Protocol,但它似乎不能反其道而行之;这就是为什么is MyProtocol.Protocol不起作用(但即使它起作用,它也不适用于派生协议;因为P.Protocol类型目前只能保存 value P.self)。
如果要检查它meta是否是等效于或派生自 的协议类型,MyProtocol可以使用 Obj-C 运行时函数protocol_conformsToProtocol:
@objc(YHMyProtocol) protocol MyProtocol { }
@objc protocol DerviedMyProtocol : MyProtocol {}
@objc class Matcher : NSObject {
@objc public class func existMatch(_ meta: Protocol) -> Bool {
return protocol_conformsToProtocol(meta, MyProtocol.self)
}
}
// the following Swift protocol types get implicitly bridged to Protocol instances
// when calling from Obj-C, @protocol gives you an equivalent Protocol instance.
print(Matcher.existMatch(MyProtocol.self)) // true
print(Matcher.existMatch(DerviedMyProtocol.self)) // true
如果您只想检查meta是否等同于MyProtocol,则可以使用protocol_isEqual:
@objc class Matcher : NSObject {
@objc public class func existMatch(_ meta: Protocol) -> Bool {
return protocol_isEqual(meta, MyProtocol.self)
}
}
print(Matcher.existMatch(MyProtocol.self)) // true
print(Matcher.existMatch(DerviedMyProtocol.self)) // false