【问题标题】:How to loop through Array<Dictionary<String,String>> using Swift [closed]如何使用 Swift 遍历 Array<Dictionary<String,String>> [关闭]
【发布时间】:2014-11-04 04:28:58
【问题描述】:

给我一​​个例子和解释“循环通过array&lt;Dictionary&lt;String,String&gt;&gt;using swift...

【问题讨论】:

  • 你需要更具体一些......请发布一些你自己的代码。
  • 您是否尝试过自己循环遍历数组?
  • 你有什么想法吗?@LyndseyScott
  • (1) “遍历数组>”可能意味着很多事情。这一点都不具体。 (2) 在发帖之前,您需要自己做出一些努力。自己编写一些代码将是一个好的开始。

标签: ios xcode swift


【解决方案1】:

这是一个显示 2 个循环的示例。第一个循环遍历数组,取出每个字典。第二个循环遍历字典,挑选出每个键值对:

let people:Array<Dictionary<String,String>> = [["first":"Fred", "last":"Jones"], ["first":"Joe", "last":"Smith"]]

// Grab each person dictionary from the array of dictionaries
for person in people {
    // Grab each key, value pair from the person dictionary
    // and print it
    for (key,value) in person {
        println("\(key): \(value)")
    }
}

这个输出:

first: Fred
last: Jones
first: Joe
last: Smith

请注意,字典是无序的,因此也可以打印:

last: Jones
first: Fred
last: Smith
first: Joe

【讨论】:

  • 不客气,阿杰。欢迎来到堆栈溢出。请勾选我的答案旁边的复选标记以接受它。
  • 我在“for person in people”行中出现错误,说“没有名为 generator 的成员”。
  • 你复制上面的代码了吗?您使用的是哪个版本的 Xcode?​​span>
  • 现在可以使用了。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 2020-02-02
  • 2016-08-16
  • 2016-10-25
  • 1970-01-01
  • 2012-03-01
  • 2012-06-16
相关资源
最近更新 更多