-1

I have a plist data, contains an array of dictionaries, and I want to get all the dictionaries first value which's english

let path = Bundle.main.path(forResource:"idictionary", ofType: "plist")
let plistData = NSArray(contentsOfFile: path!)
print(plistData![0]) // only gets the first one

printing print(plistData![0]) will only show this in the log

{
    english = abbey;
    kurdi = "\U06a9\U0644\U06ce\U0633\U06d5";
}

but I want to print all the dictionaries english value

here's a picture of my plist I have a bunch of records, and trying to get every item's english value

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sarmand Amer
  • 9
  • 1
  • 6

2 Answers2

1

First, load your data the modern way (Swift 4): declare a struct:

struct Word : Decodable { let english:String; let kurdi:String }

Load the data into an array of that struct:

let url = Bundle.main.url(forResource:"idictionary", withExtension: "plist")!
let data = try! Data.init(contentsOf: url)
let array = try! PropertyListDecoder().decode([Word].self, from: data)

Now you have an array of Word, where every Word has an english property. If you want the english only, map the array:

let englishArray = array.map{$0.english}
matt
  • 515,959
  • 87
  • 875
  • 1,141
-1

you need to loop over the array

for item in plistData {
   print(item)
}
Cory
  • 2,302
  • 20
  • 33
  • Well, thank you, your code worked, i am new to swift, don't really know if your way or @vadian's way are more efficient – Sarmand Amer Nov 22 '17 at 21:07