far2go
far2go

Reputation: 13

Failed to create NSDictionary when reading from plist in swift 2

I am trying to read info from plist, (PetList.plist) but I don't know why it failed. The variable path is correct, however myDict is nil, the else part is executed. What I want to do is load info form plist, then assign values to pets array.
var pets = Pet func loadGameData() {

    let path = NSBundle.mainBundle().pathForResource("PetList", ofType: "plist")

    let myDict = NSDictionary(contentsOfFile: path!)

    if let dict = myDict {
        //loading values
        for i in 0..<pets.count {
            pets[i].petName = dict.objectForKey("petName")!.absoluteString!
            pets[i].health = dict.objectForKey("health")!.absoluteString!
            pets[i].experience = dict.objectForKey("experience")!.absoluteString!
            pets[i].hungry = dict.objectForKey("hungry")!.absoluteString!
            pets[i].energy = dict.objectForKey("energy")!.absoluteString!
            pets[i].level = dict.objectForKey("level")!.absoluteString!
            pets[i].status = dict.objectForKey("status")!.absoluteString!
            pets[i].searchKey = dict.objectForKey("searchKey")!.absoluteString!
            pets[i].cleanliness = dict.objectForKey("cleanliness")!.absoluteString!
            pets[i].isChoosed = dict.objectForKey("isChoosed")!.absoluteString!
            pets[i].skill = dict.objectForKey("skill")!.absoluteString!
            pets[i].imageName = dict.objectForKey("imageName")!.absoluteString!
        }
    } else {
        print("WARNING: Couldn't create dictionary from PetList.plist! Default values will be used!")
    }
}

Upvotes: 0

Views: 294

Answers (2)

vadian
vadian

Reputation: 285079

There are a few issues.

  • The top level object is an array
  • absoluteString will cause an compiler error.
  • There must be a Pet struct or class to create instances from the array.

Something like this

struct Pet {
  var petName = "", health = "", experience = "", hungry = ""
  var energy = "", level = "", status = "", searchKey = ""
  var cleanliness = "", isChoosed = "", skill = "", imageName = ""
}

var pets = [Pet]()

func loadGameData() {

  let path = NSBundle.mainBundle().pathForResource("PetList", ofType: "plist")
  if let myArray =  NSArray(contentsOfFile: path!) as? [[String:String]] {
    //loading values

    for anItem in myArray {
      var pet = Pet()
      pet.petName = anItem["petName"]!
      pet.health = anItem["health"]!
      pet.experience = anItem["experience"]!
      pet.hungry = anItem["hungry"]!
      pet.energy = anItem["energy"]!
      pet.level = anItem["level"]!
      pet.status = anItem["status"]!
      pet.searchKey = anItem["searchKey"]!
      pet.cleanliness = anItem["cleanliness"]!
      pet.isChoosed = anItem["isChoosed"]!
      pet.skill = anItem["skill"]!
      pet.imageName = anItem["imageName"]!
      pets.append(pet)
    }
  } else {
    print("WARNING: Couldn't create array from PetList.plist! Default values will be used!")
  }
}

Upvotes: 1

trojanfoe
trojanfoe

Reputation: 122391

The top-level node of PetList.plist is an array, not a dictionary, which is why it's not loading.

Upvotes: 0

Related Questions