我有这个协议继承
@objc protocol Base {}
protocol Some : Base {}
检查它的代码main.swift
:
import Foundation
class Model1 : NSObject {}
extension Model1 : Some {}
class Model2 : NSObject, Some {}
func test() {
let m1: NSObject = Model1()
let m2: NSObject = Model2()
print("m1 is Some? \(m1 is Some)")
print("m2 is Some? \(m2 is Some)")
print("m1 is Base? \(m1 is Base)")
print("m2 is Base? \(m2 is Base)")
}
test()
有趣的是,swiftc
和swift
swiftc main.swift && ./main
m1 is Some? true
m2 is Some? true
m1 is Base? false
m2 is Base? false
VS
swift main.swift
m1 is Some? true
m2 is Some? true
m1 is Base? true
m2 is Base? false
但问题是 - 为什么我不能将其中任何一个投放到Base
?
如果我将@objc
添加到Some
,那么每个测试都将通过true
。但我现在不能这样做,这是另一个SO的主题(see here)。
swiftc --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9