我有来自url的下载文件的下一代码。但是我得到了下一个错误:
失败并显示错误:错误Domain = NSCocoaErrorDomain Code = 516 “”CFNetworkDownload_85Dvq4.tmp“无法移至”文档“ 因为已存在具有相同名称的项目。“ 的UserInfo = {NSSourceFilePathErrorKey = /用户/大谷/库/开发商/ CoreSimulator /设备/ 8BABC203-7FD9-4F03-BF0D-D660DD29CCDE /数据/容器/数据/应用/ 09C714D3-F185-4289-91DB-13B21557B784 / TMP / CFNetworkDownload_85Dvq4。 TMP, NSUserStringVariant =( 移动),NSDestinationFilePath = / Users / otani / Library / Developer / CoreSimulator / Devices / 8BABC203-7FD9-4F03-BF0D-D660DD29CCDE / data / Containers / Data / Application / 09C714D3-F185-4289-91DB-13B21557B784 / Documents / temp。微克, NSFilePath = /用户/大谷/库/开发商/ CoreSimulator /设备/ 8BABC203-7FD9-4F03-BF0D-D660DD29CCDE /数据/容器/数据/应用/ 09C714D3-F185-4289-91DB-13B21557B784 / TMP / CFNetworkDownload_85Dvq4.tmp, NSUnderlyingError = 0x7fea5a53a9a0 {错误域= NSPOSIXErrorDomain 代码= 17“文件存在”}}
下载文件的代码:
func downloadFile(let url: String!){
self.downloadHUD()
Alamofire.download(.GET, url) { temporaryURL, response in
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let fullPath = documentsUrl.URLByAppendingPathComponent(Misc.pathToSaveFile)
if (NSFileManager.defaultManager().fileExistsAtPath(fullPath.absoluteString)){
try! NSFileManager.defaultManager().removeItemAtURL(fullPath)
}
return fullPath
}
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
dispatch_async(dispatch_get_main_queue()) {
self.hud.progress = Float(totalBytesRead) / Float(totalBytesExpectedToRead)
print("Total bytes read on main queue: \(totalBytesRead)")
}
}
.response { _, _, _, error in
if let error = error {
print("Failed with error: \(error)")
} else {
print("Downloaded file successfully")
self.hideHUD()
}
}
}
错误上线
print("Failed with error: \(error)")
答案 0 :(得分:-2)
解决方案是重写目标文件。