我试图将数组保存到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)")
}
调试器抛出:
确实成功了吗? :false可选(" /用户/ MYNAME /库/开发商/ CoreSimulator /设备/ D573BBEC-D180-4A8A-93FE-BE04E2BBE6C8 /数据/容器/数据/应用/ 911362BF-13C2-49AB-817B-49F2926194A1 /图书馆/应用支持/测试")
为什么会错?
编辑:缩短版本,仍然没有运气:
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)