我已经声明了一个Swift协议:
protocol Option {
var name: String { get }
}
我声明了这个协议的多个实现 – 一些类,一些枚举。
我有一个视图控制器,其属性声明如下:
var options: [Option] = []
当我尝试将此属性设置为在另一个VC的prepareForSegue
中实现Option
协议的对象数组时,我收到运行时错误:
fatal error: array cannot be bridged from Objective-C
为什么这不起作用? 编译器拥有它需要的所有信息,而且我不明白Objective-C与它有什么关系 – 我的项目只包含Swift文件,并且这些数组不会进入或退出任何框架方法需要他们与NSArray
。
I have declared a Swift protocol:I declare multiple implementations of this protocol—some classes, some enums.I have a view controller with a property declared as so:When I try and set this property to an array of objects that implement the Option
protocol in another VC’s prepareForSegue
, I get a runtime error:Why doesn’t this work?The compiler has all the information it needs, and I don’t understand what Objective-C has to do with it at all—my project contains only Swift files, and these arrays aren’t coming in or out of any framework methods that would necessitate them being bridged to NSArray
.