gpt4 book ai didi

ios - Swift3 上的 "Empty collection literal requires an explicit type"错误

转载 作者:IT王子 更新时间:2023-10-29 08:14:17 50 4
gpt4 key购买 nike

我的类(class)有一个变量:

var list = []

我将它用于我的类的一个函数:

func chargeData (data: NSArray){
list = data
}

它在我的 Swift 2.3 项目中运行良好,但是当我将其更新为 XCode8Swift3 时,出现以下错误:

Empty collection literal requires an explicit type

所以我在我的列表变量中添加了一个类型转换:

var list = [] as! NSArray

但它给了我以下警告:

Forced cast of 'NSArray' to same type has no effect

我知道警报不会破坏应用程序,但我想以适当的方式解决此错误。

有人遇到同样的错误并正确解决了吗?

提前致谢!

最佳答案

发生此错误是因为隐式转换已被废除,因此您必须告诉编译器显式类型(ArrayLiteral []):

var list: NSArray = []
// or
var list = [] as NSArray

关于ios - Swift3 上的 "Empty collection literal requires an explicit type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489211/

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