gpt4 book ai didi

ios - 遍历字典键数组以检查 (Int) 键是否可用

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:45 24 4
gpt4 key购买 nike

我有一个字典数组 table

let table: [
"0": ["a": "30","b": "21"],
"1": ["a": "31","b": "22"],
"2": ["a": "32","b": "23"],
"3": ["a": "33","b": "24"],
"4": ["a": "34","b": "24"],
]

因此,我想遍历每个键(它们始终是 Int 类型(通常显示索引路径))。我可以遍历这些键吗?

喜欢:

var keys:Int = 0
// Iterate through the dictionary
for (key) in tblData {
//What should i check for here??
}

解决方案:我能够通过你的所有建议得到它,所以谢谢大家。我执行了以下操作来检查字典键是否为 Int 以及它是否可用,但我仍然无法打破循环:

let keys = (demo["data"]!["table"]!.keys).sort()

for k in keys{
var num = Int(k)
if num != nil {
print("Yeah! its an Integer")
}
else {
print("uh-oh! not an Integer")
}
}

但我仍然无法打破循环。我应该在哪里使用 break 以及条件是什么。谁能给我建议?

Swift 版本:2.3,Xcode 版本:7.3

最佳答案

试试这个:

let dic = [
"data": [
"table": [
0: [
"service_details": "(3 months)",
"bill_date": "2016-10-27"
],
1: [
"service_details": "(3 months)",
"bill_date": "2016-07-26"
],
"2": [
"service_details": "(1 year)",
"bill_date": "2015-12-29"
],
3: [
"service_details": "Installation charge",
"bill_date": "2015-07-27"
],
"4": [
"service_details": "(1 year)",
"bill_date": "2015-07-27"
]
]
]
]

//遍历字典

for (key,value) in dic["data"]!["table"]! {
if let str = key as? Int {
print("Key is --> \(key)")
print("Service Detail --> \(value["service_details"]!)")
print("Bill Date --> \(value["bill_date"]!)")
print("---------------------------------------------")
}
else {
//print("---------------If You get any other type it point here... \n index is --> \(key) \n -------------------")
}

输出:

关键是 --> 3

服务详情-->安装费

账单日期 --> 2015-07-27


关键是 --> 0

服务详情 -->(3 个月)

账单日期 --> 2016-10-27


关键是 --> 1

服务详情 -->(3 个月)

账单日期 --> 2016-07-26


关于ios - 遍历字典键数组以检查 (Int) 键是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com