Swift 检查plist文件是否存在

Swift Check if plist file exist

如何检查 plist 文件是否存在?

我正在尝试这样的事情:

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

}

但是我得到这个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

是否要在使用前检查 plist 文件是否存在?

您应该使用 URLPath,按照您的方式,没有文件夹或引用。 我假设您在主包中使用 plist。

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