文件“XXX”不存在
The file “XXX” doesn’t exist
我正在尝试使用前台互联网操作下载文件。它成功下载但问题是目录中不存在文件
代码:
let urlString = "https://joycemusic1.files.wordpress.com/2015/07/fight-song-rachel_platten.pdf"
let filePath = "Documents/"
if let url = NSURL(string: urlString){
let tast = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (NSURL, NSURLResponse, NSError) in
if let error = NSError{
print(error)
}
if let locationString = NSURL , let fileLocation = locationString.path {
//print(fileLocation)
let fileExists = NSFileManager().fileExistsAtPath("fight-song-rachel_platten.pdf")
if fileExists == true{
try! NSFileManager.defaultManager().moveItemAtPath(fileLocation, toPath: filePath)
}
}
})
tast.resume()
}
下载文件路径为:
/Users/twilight/Library/Developer/CoreSimulator/Devices/80CCC33B-817F-46C7-A65E-2B1CAA86D940/data/Containers/Data/Application/43CB754B-4096-47B1-9A97-32D1657E426B/tmp/CFNetworkDownload_EoIzdN.tmp
找不到文件
主要有两个错误。
您必须检查文件是否存在于 fileLocation
let fileExists = NSFileManager().fileExistsAtPath(fileLocation)
文件路径Documents/
根本不存在。您必须使用
将 URL 获取到沙箱中的文档文件夹
let documentsURL = try! FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
(抱歉,那是 Swift 3 码)
与错误无关,但打印错误后写入return
退出完成块。
从不 使用 class 名称 NSURL
、NSURLResponse
、NSError
作为变量名称,使用 url
、response
、error
。
对于Swift 4.0 |使用以下代码:
您必须检查文件是否存在于 fileLocation
let fileExists : Bool = FileManager.default.fileExists(atPath: fileLocation)
文件路径Documents/根本不存在。您必须使用
将 URL 获取到沙箱中的文档文件夹
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dataPath = documentsDirectory.appendingPathComponent(fileLocation)
我正在尝试使用前台互联网操作下载文件。它成功下载但问题是目录中不存在文件
代码:
let urlString = "https://joycemusic1.files.wordpress.com/2015/07/fight-song-rachel_platten.pdf"
let filePath = "Documents/"
if let url = NSURL(string: urlString){
let tast = NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (NSURL, NSURLResponse, NSError) in
if let error = NSError{
print(error)
}
if let locationString = NSURL , let fileLocation = locationString.path {
//print(fileLocation)
let fileExists = NSFileManager().fileExistsAtPath("fight-song-rachel_platten.pdf")
if fileExists == true{
try! NSFileManager.defaultManager().moveItemAtPath(fileLocation, toPath: filePath)
}
}
})
tast.resume()
}
下载文件路径为:
/Users/twilight/Library/Developer/CoreSimulator/Devices/80CCC33B-817F-46C7-A65E-2B1CAA86D940/data/Containers/Data/Application/43CB754B-4096-47B1-9A97-32D1657E426B/tmp/CFNetworkDownload_EoIzdN.tmp
找不到文件
主要有两个错误。
您必须检查文件是否存在于
fileLocation
let fileExists = NSFileManager().fileExistsAtPath(fileLocation)
文件路径
将 URL 获取到沙箱中的文档文件夹Documents/
根本不存在。您必须使用let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
(抱歉,那是 Swift 3 码)
与错误无关,但打印错误后写入
return
退出完成块。
从不 使用 class 名称 NSURL
、NSURLResponse
、NSError
作为变量名称,使用 url
、response
、error
。
对于Swift 4.0 |使用以下代码:
您必须检查文件是否存在于 fileLocation
let fileExists : Bool = FileManager.default.fileExists(atPath: fileLocation)
文件路径Documents/根本不存在。您必须使用
将 URL 获取到沙箱中的文档文件夹let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let dataPath = documentsDirectory.appendingPathComponent(fileLocation)