[英]“Type'Program' does not conform to protocol 'Any Object'”
I updated xcode and now I have error in my project and I dont have idea what to do with it. 我更新了xcode,现在我的项目出错,我不知道如何处理它。
struct Program {
let name : String
let url : String
}
self.arrayOfPrograms = [Program(name: "First", url: "http://1.com"), Program(name: "Second", url: "http://2.com"), Program(name: "Third", url: "http://2.com")]
and I'm getting error "Type'Program' does not conform to protocol 'Any Object'" 我收到错误“Type'Program'不符合协议'任何对象'”
As reported in the documentation : 如文档中所述:
AnyObject can represent an instance of any class type.
AnyObject可以表示任何类类型的实例。
A struct is not a class, so it cannot be cast to AnyObject
struct不是类,因此无法
AnyObject
为AnyObject
You should either: 你应该:
Program
into a class Program
变成一个类 Array<Any>
Array<Any>
Program
only, declare it as Array<Program>
Program
实例,请将其声明为Array<Program>
Needless to say, the last is the best solution, whereas the first is the one I wouldn't recommend because it requires you to make design changes (there's a reason why you declared it as a value type and not a reference type). 不用说,最后一个是最好的解决方案,而第一个是我不推荐的解决方案,因为它需要你进行设计更改(这就是为什么你将它声明为值类型而不是引用类型的原因)。
Side note: arrays and dictionaries can be cast to AnyObject
because they are automatically bridged respectively to NSArray
and NSDictionary
, which are classes. 旁注:数组和字典可以转换为
AnyObject
因为它们分别自动桥接到NSArray
和NSDictionary
,它们是类。
check this link you got your answare I recently wrapped my head around something that I have found very strange in Swift.
检查这个链接你得到了你的answare我最近把我的脑袋包裹在Swift中我发现很奇怪的东西。 Swift provides two high level protocols called Any and AnyObject.
Swift提供了两个名为Any和AnyObject的高级协议。 Any can be used for both value types (like structs) and reference types (classes) while AnyObject can only be used for classes.
任何可以用于值类型(如结构)和引用类型(类),而AnyObject只能用于类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.