Sam Sam - 2 years ago 183
Swift Question

Swift - read plist file to an array?

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 this?

enter image description here

I have also used a localizable.strings to retrieve the value "the cat" for "el gato" but I would like to retrieve this from englishArray.plist

I started off with this but not sure if I'm on the right path

let path = NSBundle.mainBundle().pathForResource("englishArray", ofType: "plist")
let plistEnglishArray = NSArray(contentsOfFile: path!)

Here is the rest of my code:

var englishArray: [String] = ["rainbow", "boots", "heart", "leaf", "umbrella", "tired", "angry", "cry", "the cat" ]

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.

self.translateTextField.delegate = self
picker.delegate = self
picker.dataSource = self

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

@IBAction func translateButtonTapped(sender: UIButton) {

let emptyString = self.translateTextField.text

if (emptyString!.isEmpty) {

print("please enter a word")


for transIndex in englishArray.indices {
if englishArray[transIndex] == emptyString!.lowercaseString {


//translateLabel.text = "\(spanishArray[transIndex])"
translateLabel.text = NSLocalizedString(emptyString!.lowercaseString, comment:"")



Answer Source

Change your root object to Array, then

var myEnglishArray: [String] = []
if let URL = NSBundle.mainBundle().URLForResource("englishArray", withExtension: "plist") {
      if let englishFromPlist = NSArray(contentsOfURL: URL) as? [String] {
        for myEnglish in englishFromPlist {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download