【问题标题】:loop through an array of dictionary keys for checking whether (Int) keys are available循环遍历字典键数组以检查 (Int) 键是否可用
【发布时间】:2017-04-27 05:03:30
【问题描述】:

我有一个 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 类型(通常它显示 indexpath))。我可以循环遍历这些键吗?

喜欢:

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")
  }
}

但我仍然无法打破循环。我应该在哪里使用休息,应该是什么条件。谁能给我推荐一下?

Swift 版本:2.3, Xcode 版本:7.3

【问题讨论】:

标签: ios swift loops dictionary


【解决方案1】:

如果tblData 是一个字典,那么你可以使用这个循环遍历所有的键:

for key in dic.keys {
    print(dic[key])
}

【讨论】:

  • 好的,我们可以做些什么来检查给定的键是否是整数,并只打印键为 Int 1,2,3,4,5,6.. 的值以及当键不是一个 Int,而不是通过字典的循环停止。
【解决方案2】:

试试这个:

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


【讨论】:

  • 好的,我们可以做些什么来检查给定的键是否是整数,并只打印键为 Int 1,2,3,4,5,6.. 的值以及当键不是一个 Int,而不是通过字典的循环停止。
  • 再次验证,如果有任何查询,请提供您的字典。
  • 检查key是否为Int,如果不是Int则使用break关键字停止循环
  • 但是,您在哪里检查字典键是否为 Int 类型?
  • 我实际上已经修改了代码@PiyushSanepara 并这样做了: var keysData = [tableData]() let keys = tblData.keys.sort() for k in keys{ let num = Int (k) if num != nil { if let dictData = Mapper().map(tblData[k]){ print(dictData) keysData.append(dictData) } } } 但是,我不能打破字典键结束时的循环。
【解决方案3】:

你可以这样做

let keyToCheck = 3

let mainDict = [String:AnyObject]() // your JSON data dictionary
let dataDict = mainDict["data"] as! [String:AnyObject]
let tableDict = dataDict["table"] as! NSDictionary

let allKeys = tableDict.allKeys as! [String]

if allKeys.contains("\(keyToCheck)")
{
   print("Key is available")
   let keyData = tableDict.objectForKey("\(keyToCheck)") as! NSDictionary
   print("\(keyData)")
}
else
{
   print("Key not available")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    相关资源
    最近更新 更多