如何在swift 3.0中将数组中的字典作为索引追加

时间:2018-01-04 09:26:30

标签: swift3

如何添加字典的键,数组中的值作为数组索引形式,如下所示

[
    {
      "summary": "fdsfvsd"
    },
    {
      "content_date": "1510158480"
    },
    {
      "content_check": "yes"
    }
]

1 个答案:

答案 0 :(得分:0)

根据您的问题,您希望在Swift中使用字典数组, 这是实现相同目标的简单方法:

  var arrayOfDict = [[String: String]]()

//creating dictionaries      
         let dict1 = ["name" : "abc" , "city" : "abc1"]
         let dict2 = ["name" : "def" , "city" : "def1"]
         let dict3 = ["name" : "ghi" , "city" : "ghi1"]
         let dict4 = ["name" : "jkl" , "city" : "jkl1"]

//Appending dictionaries to array

         arrayOfDict.append(dict1)
         arrayOfDict.append(dict2)
         arrayOfDict.append(dict3)
         arrayOfDict.append(dict4)


//accessing each and every element of the arrayOfDictionary

         for dict in arrayOfDict{
              for (key, value) in dict {
                   print("the value for \(key) is = \(value)")
              }
         }

希望它对你有所帮助!