继承自@objc协议

时间:2017-02-03 12:09:27

标签: swift inheritance

我有这个协议继承

@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()

有趣的是,swiftcswift

的输出不同
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

1 个答案:

答案 0 :(得分:0)

来自swift-users,似乎是一个已知问题,需要修复:https://bugs.swift.org/browse/SR-1175

现在也报告为https://bugs.swift.org/browse/SR-3882