【问题标题】:How to append dictionary in array as index in swift 3.0如何在 swift 3.0 中将数组中的字典作为索引附加
【发布时间】:2018-01-04 09:26:29
【问题描述】:

如何在数组中添加字典的键、值作为数组索引的形式,像这样

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

【问题讨论】:

  • 你能扩展这个问题吗?不清楚你到底想要什么。
  • 我想在数组中附加一个字典,该字典在特定索引上有键值
  • 不要在 cmets 中发布代码 - 编辑您的问题。

标签: swift3


【解决方案1】:

根据您的问题,您想在 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)")
              }
         }

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2015-08-30
    • 2021-04-24
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多