How to create an array of dictionaries?

那年仲夏 提交于 2019-12-24 03:14:36

问题


new to programming!

I'm trying to create an array of dictionaries inside a struct in Swift like so:

var dictionaryA = [
    "a": "1",
    "b": "2",
    "c": "3",
    ]
var dictionaryB = [
    "a": "4",
    "b": "5",
    "c": "6",
    ]
var myArray = [[ : ]]
myArray.append(dictionaryA)
myArray.append(dictionaryB)

This works fine in a playground, but when I put it into an Xcode project, inside a struct, the lines with the append function produce the error "Expected declaration".

I've also tried using the += operator with the same result.

How can I successfully construct this array inside the struct?


回答1:


From your error Expected declaration, I assume you are doing like:

struct Foo {
    var dictionaryA = [
        "a": "1",
        "b": "2",
        "c": "3",
    ]
    var dictionaryB = [
        "a": "4",
        "b": "5",
        "c": "6",
    ]
    var myArray = [[ : ]]
    myArray.append(dictionaryA) // < [!] Expected declaration
    myArray.append(dictionaryB)
}

This is because you can place only "declarations" in the struct body, and myArray.append(dictionaryA) is not a declaration.

You should do that somewhere else, for example in the initializer. The following code compiles.

struct Foo {
    var dictionaryA = [
        "a": "1",
        "b": "2",
        "c": "3",
    ]
    var dictionaryB = [
        "a": "4",
        "b": "5",
        "c": "6",
    ]
    var myArray = [[ : ]]

    init() {
        myArray.append(dictionaryA)
        myArray.append(dictionaryB)
    }
}

But as @AirspeedVelocity mentioned, you should provides more information about myArray, or myArray would be Array<NSDictionary> which I think you don't expect.

Anyway, the correct solution would vary depending on what you really trying to do:

Maybe or maybe not, what you want is something like:

struct Foo {
    static var dictionaryA = [
        "a": "1",
        "b": "2",
        "c": "3",
    ]
    static var dictionaryB = [
        "a": "4",
        "b": "5",
        "c": "6",
    ]

    var myArray = [dictionaryA, dictionaryB]
}

But, I don't know, why don't you just:

struct Foo {

    var myArray = [
        [
            "a": "1",
            "b": "2",
            "c": "3",
        ],
        [
            "a": "4",
            "b": "5",
            "c": "6",
        ]
    ]
}



回答2:


The problem lies with this line:

var myArray = [[ : ]]

You need to tell Swift what type myArray is – [[:]] isn’t enough information.

You can either do it the explicit way:

var myArray: [[String:String]] = [[ : ]]

Or, if practical, implicitly using the first or both values you plan to put in:

var myArray = [dictionaryA]
var myArray = [dictionaryA,dictionaryB]

(as an alternative to the explicit empty version, you can also write var myArray = [[String:String]](), which is shorthand for var myArray = Array<Dictionary<String,String>>())




回答3:


var arrayOfDict = [[String: Int]]()
// Create a dictionary and add it to the array.
var dict1: [String: Int] = ["age": 20]
arrayOfDict.append(dict1)
// Create another dictionary.
var dict2: [String: Int] = ["rank": 5].
arrayOfDict.append(dict2)
// Get value from dictionary in array element 0.
if let value = arrayOfDict[0]["age"] {
print(value)
}

Output

20




回答4:


Or you can use an array of tuples that´s even easier, like this:

var myArray:[(a:String,b:String,c:String)] = []

And append any element you need later:

self.myArray.append((a:"A",b:"B",c:"c"))

And to use them just:

self.myArray[index].a
self.myArray[index].b
self.myArray[index].c


来源:https://stackoverflow.com/questions/28410218/how-to-create-an-array-of-dictionaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!