首页 > 解决方案 > 在 swift 中使用 for 循环创建字典

问题描述

我只想在 for 循环示例代码的帮助下创建一个字典:

var counter: Int = 1;
var pageCountDict = [String:Any]();
    for filterCount in counter..<6
    {
       if let count = "page_\(filterCount)_vtime" as? String
       {
          pageCountDict = [count: timeInterval_Int];
       }
    }
print(pageCountDict);

这个打印命令只给我forloop的最后一个值
我只想要这个变量pageCountDict的所有值在一个字典中

标签: iosarraysswiftdictionary

解决方案


分配给字典的方法是首先使用下标并为其分配值:

pageCountDict[YourKey] = YourValue

此外,您可以在Apple 文档中看到许多关于字典的示例和解释。


推荐阅读