I'm really confused on this one. There are dozens of questions around the web asking "How do I get info from my plist file in Swift?" and the same answer is posted everywhere:
let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist")
However, this line produces always produces nil
for me. I have replaced Config
with other components found in the default plist file, but get nil
as well.
I am trying to access my custom ProductIdentifiers
Array like so:
let url = NSBundle.mainBundle().URLForResource("ProductIdentifiers", withExtension: "plist")!
var productArray = NSArray(contentsOfURL: url) as! [[String:AnyObject!]]
I get a crash stating fatal error: unexpectedly found nil while unwrapping an Optional value
on productArray. I have also tried this with other default plist values in place of ProductIdentifiers
.
Does anyone know why this is not working for me even though there are so many posts around of people using this successfully?
I've never heard of the OP's approach working before. Instead, you should open the
Info.plist
file itself, then extract values from it, like so:Swift 3.0+
Swift 2.0
Resource
represents the file name of the plist rather than its contents.The root object of the plist is probably a dictionary.
Replace
MyPlist
with the real file name. This code prints the contents of the plist