pableiros pableiros - 3 months ago 25
Swift Question

Swift Check if plist file exist

How can I check if a

plist
file exists?

I'm trying something like this:

if let dictFile = NSDictionary(contentsOfURL: "file.plist") {

}


But I get this error:


fatal error: unexpectedly found nil while unwrapping an Optional value


Is anyway to check if a
plist
file exist before use it?

Answer

You should use a URLPath, in the way you are doing, there's no folder or reference. I'm assuming that you're using a plist in the main bundle.

if let filePath = NSBundle.mainBundle().pathForResource("file", ofType: "plist"), let dataDictionary = NSDictionary(contentsOfFile: filePath){
           .
           . 
           .
        }