Stella
Stella

Reputation: 1868

Reading keys in plist and get the values

I have the following plist file in my Swift iOS project. I want to read the keys without specifically mention and get the value for all the keys. I want to write this in Swift code. I tried the below code and getting all the keys and values. But, I want to check for each key(not mentioned specifically like "1234" etc.) and get the value for it. Please help how to achieve this?

var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("UserDetails", ofType: "plist") {
      myDict = NSDictionary(contentsOfFile: path)
      NSLog("myDict: %@", myDict!)
}

enter image description here

Upvotes: 1

Views: 455

Answers (2)

Eric Aya
Eric Aya

Reputation: 70098

You can use a tuple to loop in a dictionary:

for (key, value) in myDict! {
    println("Key is \(key) and value is \(value)")
}

Upvotes: 1

qwerty_so
qwerty_so

Reputation: 36313

Just use this code

for (key, val) in myDict! {
  println("\(key):\(val)")
}

to print the single keys/values.

Upvotes: 1

Related Questions