Search code examples
iosswiftplist

Unexpectedly found nil while unwrapping an Optional value - Plist


i am trying to read from propriety list using swift but im getting this error, and thats the code im using to read from my plist :

Arrays i'm using :

var recipeNames :[String] = []
var recipeImages :[String] = []
var recipeTime :[String] = []

In viewDidload :

 var path = NSBundle.mainBundle().pathForResource("recipes", ofType: "plist")
    var dict = NSDictionary(contentsOfFile: path!)
     recipeNames  = dict!.objectForKey("name")! as! [String]
     recipeImages  = dict!.objectForKey("image")! as! [String]
     recipeTime  = dict!.objectForKey("time")! as! [String]

Solution

  • Seems to be no error in your code, Check that the arrays in plist has the same naming as in your code might be mistaken something with keys.

    You can check that by right-click on plist and Open-as then choose source code

    Like : objectForKey("name") called <key>name</key> objectForKey("image") called <key>image</key> objectForKey("time") called <key>time</key>