Aashish
Aashish

Reputation: 2706

loop through an array of dictionary keys for checking whether (Int) keys are available

I have an array of dictionaries of 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"],
            ]

So, I would like to loop through each of the keys (which will always be of type Int (Generally it is displaying indexpath)). Can I loop through those keys?

Like:

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

Solution: I was able to get it with all of your suggestions, So thank you everyone guys. I did the following to check whether the dictionary key is either a Int and whether it is available or not but I still can't break the loop:

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

But I'm still not able to break the loop. Where should I use the break and what should be the condition. can anyone please suggest me?

Swift version: 2.3, Xcode version: 7.3

Upvotes: 1

Views: 1120

Answers (3)

Piyush Sanepara
Piyush Sanepara

Reputation: 1417

Try this:

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"
            ]
        ]
    ]
]

// Iterate through the dictionary

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

}

Output:

Key is --> 3

Service Detail --> Installation charge

Bill Date --> 2015-07-27


Key is --> 0

Service Detail --> (3 months)

Bill Date --> 2016-10-27


Key is --> 1

Service Detail --> (3 months)

Bill Date --> 2016-07-26


Upvotes: 2

Ravi
Ravi

Reputation: 2451

you can do like this

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

Upvotes: 0

Vishal
Vishal

Reputation: 2060

If tblData is a dictionary, then you can loop through all the keys using this:

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

Upvotes: 0

Related Questions