使用writeToURL方法将数组保存到文件中

时间:2015-09-13 13:23:59

标签: ios nsfilemanager data-persistence create-directory

我试图将数组保存到Application Support文件夹中的文件。 这是我正在使用的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    let fileManager = NSFileManager()

    var suppUrl = try! fileManager.URLForDirectory(NSSearchPathDirectory.ApplicationSupportDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true)
    suppUrl = suppUrl.URLByAppendingPathComponent("test")

    try! fileManager.createDirectoryAtURL(suppUrl, withIntermediateDirectories: true, attributes: nil)
    print(suppUrl.path)


    let arrayToSave: [String] = ["1", "2", "3", "4", "5"]
    let success = NSArray(array: arrayToSave).writeToURL(suppUrl, atomically: true)
    print("did succeed?:  \(success)")

}

调试器抛出:

  

可选(" /用户/ MYNAME /库/开发商/ CoreSimulator /设备/ D573BBEC-D180-4A8A-93FE-BE04E2BBE6C8 /数据/容器/数据/应用/ 911362BF-13C2-49AB-817B-49F2926194A1 /图书馆/应用支持/测试")

确实成功了吗? :false

为什么会错?

编辑:缩短版本,仍然没有运气:

let fileManager = NSFileManager()
        var suppUrl = try! fileManager.URLForDirectory(NSSearchPathDirectory.ApplicationSupportDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true)

        let arrayToSave: [String] = ["1", "2", "3", "4", "5"]
        let success = NSArray(array: arrayToSave).writeToURL(suppUrl.URLByAppendingPathComponent("test.plist"), atomically: true)

0 个答案:

没有答案