gpt4 book ai didi

类里面的 Swift 词典

转载 作者:行者123 更新时间:2023-11-30 10:16:48 28 4
gpt4 key购买 nike

我在 Swift 类中的字典数组方面遇到问题。我的代码无法在类或结构中运行,但可以在外部运行。

var data = [Dictionary<Int,String>]()
data.append([123: "test"])

println(data[0])
// Working OK!

class DTest {
var data = [[Dictionary<Int,String>]]()

func check() {
data.append([123: "test"])
// Error: Cannot invoke "append" with an argument list of type '([Int : String])'
data += [123: "test"]
// Error: Binary operator += can't be applied to operands of type ...
}
}

最佳答案

这是因为您已将类中的data声明为字典数组的数组:

错误:

var data = [[Dictionary<Int,String>]]()

很好:

var data = [Dictionary<Int,String>]()

关于类里面的 Swift 词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29559479/

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