0

我在 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 ...
    }
}
4

1 回答 1

1

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

错误的:

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

美好的:

var data = [Dictionary<Int,String>]()
于 2015-04-10T10:51:58.730 回答