gpt4 book ai didi

ios - URLSession didFinishDownloadingToURL 后移动文件失败

转载 作者:行者123 更新时间:2023-11-30 14:18:34 25 4
gpt4 key购买 nike

我在模拟器上运行,出现此错误:

file:///Users/hunterp/Library/Developer/CoreSimulator/Devices/asd3243423-A1A4-4CFB-8A8F-234asdasd/data/Containers/Data/Application/4564561we-5FC5-4718-843B-sdasdwq4134/tmp/CFNetworkDownload_o0xtzV.tmp Moved failed with error: The operation couldn’t be completed. (Cocoa error 4.)

我这样做:

    var test = DownloadClass(url: s, completionBlock: { (location) -> () in
println (location)
var toPath = self.getDocPath().stringByAppendingPathComponent("s.jpg")
self.moveFile(location.absoluteString!, toThePath: toPath )
println ("DONE")
})
test.start()

地点:

   func moveFile(fromPath: String, toThePath: String) {

var error: NSError?
let filemgr = NSFileManager.defaultManager()

if filemgr.moveItemAtPath(fromPath, toPath: toThePath, error: &error) {
println("Move successful")
} else {
println("Moved failed with error: \(error!.localizedDescription)")
}
}

func getDocPath() -> String {
return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
}

class DownloadClass: NSObject,NSURLSessionDelegate,NSURLSessionDownloadDelegate {
var session:NSURLSession?
var downloadUrl:String
var completion:(NSURL)->()
init(url:String,completionBlock:(location:NSURL)->()){
completion = completionBlock
downloadUrl = url
super.init()
session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: nil)
}
func start(){
let request = NSURLRequest(URL: NSURL(string: downloadUrl)!);
let downloadTask = session!.downloadTaskWithRequest(request)
downloadTask.resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
completion(location)
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
//here get progress
}
}

最佳答案

将移动路径代码更改为此,它将起作用

self.moveFile(location.path!, toThePath: toPath )

关于ios - URLSession didFinishDownloadingToURL 后移动文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com