我正在使用 NSURLDownload 在 Mac 临时文件夹中下载一个 zip 文件。这是代码:
func function () {
var request:NSURLRequest = NSURLRequest(URL: NSURL(string: self.downloadLink.stringValue)!)
var download:NSURLDownload = NSURLDownload(request: request, delegate: self)
}
func download(download: NSURLDownload, decideDestinationWithSuggestedFilename filename: String) {
tempPath = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString)
download.setDestination(tempPath.stringByAppendingPathExtension("zip")!, allowOverwrite: false)
}
这可行,但我试图通过附加路径组件将我的 zip 下载隔离到我刚刚创建的临时文件夹中:
tempPath = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString).stringByAppendingPathComponent("thisShouldBeTheNameOfTheFile")
在这种情况下,下载不起作用,不会创建任何内容,也不会调用函数 downloadDidFinish。
临时目录是否受到保护,所以我无法在其中创建新文件夹?我怎样才能解决这个问题?
最佳答案
您还可以创建如下所示的字符串扩展:
extension String {
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.stringByAppendingPathComponent(path)
}
}
然后像这样使用它:
let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("<your string value>")
希望这对您有所帮助!
关于macos - 无法将路径组件附加到 Swift 中的临时目录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30575757/