Swift保存/重新加载数据到plist

时间:2014-11-08 14:27:36

标签: iphone swift ios8

我在项目文件夹中输入了samplePlist.plist,我正在尝试将数据保存在这个.....中

 var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) //Get Path of Documents Directory
        var documentsDirectory:AnyObject = paths[0]
        var path = documentsDirectory.stringByAppendingPathComponent("samplePlist.plist")
        var fileManager = NSFileManager.defaultManager()
        var fileExists:Bool = fileManager.fileExistsAtPath(path)
        var data : NSMutableDictionary?
        //Check if plist file exists at path specified
        if fileExists == false {
            //File does not exists
            data = NSMutableDictionary () //Create data dictionary for storing in plist      
        } else  {
            //File exists – retrieve data from plist inside data dictionary
            data = NSMutableDictionary(contentsOfFile: path)
        }
        data?.setValue("\(countButton)", forKey: "NumeroButton")
        data?.writeToFile(path, atomically: true) //Write data to file permanently

和我读到了plist

//Get path of Documents directory
    var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    var documentsDirectory:AnyObject = paths[0]
     var path = documentsDirectory.stringByAppendingPathComponent("samplePlist.plist")
//Retrieve contents from file at specified path
    var data = NSMutableDictionary(contentsOfFile: path!)
   println(path)

我的问题是,如果我尝试在我的iphone上启动它,我不会在启动应用程序时将文件上传到My Documents文件夹

  

/var/mobile/Containers/Data/Application/9CCDBD0B-FA29-4C9E-910E-9AD5F5B11E5A/Documents/samplePlist.plist   致命错误:在解包可选值时意外发现nil

0 个答案:

没有答案