将.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
通过调试器:
file:///Users/Ric/Library/Developer/CoreSimulator/Devices/ <device id>/data/Containers/Data/Application/<app id>/Documents/
原因(通过错误):
“The operation couldn’t be completed. Is a directory”
所以问题:为什么以下不起作用?
docURL.URLByAppendingPathComponent("/RicFile.txt")
URLByAppendingPathComponent:不会改变现有的NSURL,它会创建一个新的NSURL.从
documentation:
原文链接:https://www.f2er.com/swift/319035.htmlURLByAppendingPathComponent: Returns a new URL made by appending a
path component to the original URL.
您需要将方法的返回值赋给某些东西.例如:
let directoryURL = manager.URLForDirectory(.DocumentDirectory,error:&error) let docURL = directoryURL.URLByAppendingPathComponent("/RicFile.txt")
更好的方法是使用NSURL(string:String,relativeTo:NSURL):
let docURL = NSURL(string:"RicFile.txt",relativeTo:directoryURL)