gpt4 book ai didi

ios - "Type' 程序 ' does not conform to protocol ' 任何对象 '"

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:02 24 4
gpt4 key购买 nike

我更新了 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")]

我收到错误“Type'Program' does not conform to protocol 'Any Object'”

最佳答案

documentation 中所述:

AnyObject can represent an instance of any class type.

结构不是类,因此不能转换为 AnyObject

你应该:

  • Program上课
  • 定义你的数组为Array<Any>
  • 如果你的数组应该包含 Program 的实例只是,将其声明为 Array<Program>

不用说,最后一个是最好的解决方案,而第一个是我不推荐的,因为它需要您进行设计更改(您将其声明为值类型而不是引用类型是有原因的).

旁注:数组和字典可以转换为 AnyObject因为它们分别自动桥接到 NSArrayNSDictionary , 这是类。

关于ios - "Type' 程序 ' does not conform to protocol ' 任何对象 '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530691/

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