gpt4 book ai didi

swift - 继承自@objc协议(protocol)

转载 作者:可可西里 更新时间:2023-10-31 23:13:04 24 4
gpt4 key购买 nike

我有这个协议(protocol)继承

@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

对比

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

最佳答案

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

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

关于swift - 继承自@objc协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com