Swift: dictionaries inside array

前端 未结 4 853
半阙折子戏
半阙折子戏 2021-01-16 04:08

Data:

[
    { firstName: \"Foo\", lastName: \"Bar\" },
    { firstName: \"John\", lastName: \"Doe\" }
]

How can I have this kind of structu

4条回答
  •  抹茶落季
    2021-01-16 04:12

    Which version of Xcode have you got? Your code should work fine but the line:

    var persons:Array = [Dictionary()]
    

    create the array with first empty dictionary, try this instead:

    var persons:Array = [Dictionary]()
    
    var dic1 = ["Name" : "Jon"]
    var dic2 = ["Surname" : "Smith"]
    
    persons.append(dic1)
    persons.append(dic2)
    
    println(persons)
    

提交回复
热议问题