import Foundation
import Swift
struct HasType<Type: AnyObject> {
static func inObject<T>(object: T) -> Bool {
return object is Type
}
}
@objc protocol MyBaseProtocol : class {}
@objc protocol MyDerivedProtocol : MyBaseProtocol {}
@objc class MyBaseClass : MyDerivedProtocol {}
@objc class MyDerivedClass : MyBaseClass {}
let mbc = MyBaseClass()
let mdc = MyDerivedClass()
HasType<MyBaseProtocol>.inObject(mbc) // True
HasType<MyDerivedProtocol>.inObject(mbc) // True
HasType<MyBaseProtocol>.inObject(mdc) // False
HasType<MyDerivedProtocol>.inObject(mdc) // False
HasType<MyBaseClass>.inObject(mdc) // True
HasType<MyDerivedClass>.inObject(mdc) // True
爲什麼這個方法返回2個假。我期待All True。我錯過了什麼?所有這些代碼可以在操場上派生類不符合協議