我在获取 Dictionary
的键数组时遇到一些问题 - 这将用于 UITableView
数据模型
对于顶层表,我期望计数为 3 和键:GMRS、IRLP 和 WinSystem
代码:
let rows = repeaterInfoDict.count // 3
let keyArray = repeaterInfoDict.allKeys // Error: [String:[String:[String:Any]]] does not have a member named 'allKeys'
.count 收到 3,但 .allKeys 因 Xcode 错误而出错:[String:[String:[String:Any]]] does not have a member named 'allKeys'
字典的一些细节:
repeaterInfoDict 定义:
var repeaterInfoDict = String:[String:[String:Any]]
AWS CognitoSync 完成同步后字典的样子:
控制台:
print ("repeaterInfoDict:\(repeaterInfoDict)")
- 添加了空白格式以便于查看。
repeaterInfoDict:
[
GMRS:
[
1121: [no: "San Jose,CA", pl: 0.0, lt: 37.33939, fq: 462.550, ln: -121.89496, ca: "WPUI299"],
1011: [no: "Napa,CA", pl: 0.0, lt: 38.29714, fq: 462.575, ln: -122.28553, ca: "wqbk788"],
1104: [no: "Palmdale,CA", pl: 0.0, lt: 34.57943, fq: 462.65, ln: -118.11646, ca: "WQHR999"]
],
IRLP:
[
1121: [no: "St. Andrews,MB", pl: 127.3, lt: 114.3245, fq: 0.62, ln: 33.2345, ca: "VE4COR"],
1011: [no: "Birken,BC", pl: 100.0, lt: 114.3245, fq: 147.0, ln: 33.2345, ca: "VA7IP"],
1104: [no: "Saskatoon,SK", pl: 100.0, lt: 52.11679, fq: 446.1, ln: -106.63452, ca: "VE5RNP"]
],
WinSystem:
[
1121: [no: "Los Angeles,CA", pl: -100.0, lt: 34.05223, fq: 448.06, ln: -118.24368, ca: "K6JSI"],
1011: [no: "Escondido,CA", pl: -100.0, lt: 33.11921, fq: 449.08, ln: -117.08642, ca: "K6JSI"],
1104: [no: "San Diego,CA", pl: -100.0, lt: 32.71533, fq: 447.64, ln: -117.15726, ca: "K6JSI"]
]
]
print ("repeaterInfoDict.count: \(repeaterInfoDict.count)")
repeaterInfoDict.count: 3
- 在第一级表中,我希望有 3 行和键:GMRS、IRLP、WinSystems
- 在 IRLP 的二级表中,我希望有 3 行和键:1121、1011、1104
- 在 1121 的第三级表中,我希望格式化
Dictionary
[编号:“加利福尼亚州洛杉矶”,PL:-100.0,LT:34.05223,FQ:448.06,LN: -118.24368, ca: "K6JSI"] 进入自定义单元格/ View
在这个嵌套的 Dictionary
中获取键的正确/最佳方法是什么
最佳答案
我猜您不需要将所有这些都放在一个数组中。然后,我建议您使用从 keys
属性(而不是 allKeys
)收到的键遍历这些项目。
for key in dictionary.keys {
for nestedKey in dictionary[key].keys {
for nested2Key in dictionary[key][nestedKey].keys {
// use the keys
}
}
}
请记住,这不会像您输入的那样排序,Dictionary
不存储顺序。
关于ios - 字典没有名为 'allKeys' 的成员 - swift2 获取所有字典键失败并出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31019167/