将.txt文件组件附加到URL路径不起作用:
var error:NSError? let manager = NSFileManager.defaultManager() let docURL = manager.URLForDirectory(.DocumentDirectory,inDomain:.UserDomainMask,appropriateForURL:nil,create:true,error:&error) docURL.URLByAppendingPathComponent("/RicFile.txt") <-- doesn't work
通过调试器:@H_403_4@
file:///Users/Ric/Library/Developer/CoreSimulator/Devices/ <device id>/data/Containers/Data/Application/<app id>/Documents/
使用docURL将字符串写入文件不起作用,因为缺少文件名.@H_403_4@
“The operation couldn’t be completed. Is a directory”@H_403_4@
所以问题:为什么以下不起作用?@H_403_4@
docURL.URLByAppendingPathComponent("/RicFile.txt")
URLByAppendingPathComponent:不会改变现有的NSURL,它会创建一个新的NSURL.从
documentation:
URLByAppendingPathComponent: Returns a new URL made by appending a
path component to the original URL.@H_403_4@
let directoryURL = manager.URLForDirectory(.DocumentDirectory,error:&error) let docURL = directoryURL.URLByAppendingPathComponent("/RicFile.txt")
更好的方法是使用NSURL(string:String,relativeTo:NSURL):@H_403_4@
let docURL = NSURL(string:"RicFile.txt",relativeTo:directoryURL)