Swift - read plist file to an array?

后端 未结 5 1769
忘掉有多难
忘掉有多难 2021-02-08 10:55

I have created a mini translation from English words to Spanish words. I would like to use the englishArray.plist instead of my englishArray = [\"the cat\"] How can I create thi

5条回答
  •  清酒与你
    2021-02-08 11:21

    Here is the solution for swift 3. For this solution you do not need to change types in your plist structure (keep Dictionary, Array, as is). Also note that since your array's name in plist is also englishArray so the (value for key) argument in the second if statement is also englishArray.

    var myEnglishArray: [String] = []
       if let URL = Bundle.main.url(forResource: "englishArray", withExtension: "plist") {
          guard let englishFromPlist = NSDictionary(contentsOf: URL) else { return [] }
          if let englishArray = englishFromPlist.value(forKey: "englishArray") as? [String] {
             for myEnglish in englishArray {
                myEnglishArray.append(myEnglish)
             }
          }
       }
    

提交回复
热议问题