无法使用Swift移动文件

时间:2015-06-06 21:44:27

标签: ios swift

为什么会出现此错误?

  

已移动失败并显示错误:无法完成操作。 (可可错误4.)   零   文件:///Users/hunterp/Library/Developer/CoreSimulator/Devices/89271982-A1A4-24234-8A8F-799FAED4E923/data/Containers/Data/Application/9BCF6922-A3A5-46E8-9C9E-281921323/tmp/CFNetworkDownload_1i5wz3.tmp

    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
    }

 let task =  NSURLSession.sharedSession().downloadTaskWithURL(url) { fileURL, response, error in
     var toPath = self.getDocPath().stringByAppendingPathComponent("s.jpg")
     self.moveFile(fileURL.absoluteString!, toThePath: toPath )
}

1 个答案:

答案 0 :(得分:0)

您收到No Such File错误。您要求在从网络下载文件时移动看似由操作系统生成的临时文件。当您尝试移动文件时,系统可能已删除该文件。

诊断问题的一种方法是在您要移动文件时设置断点。打印出文件路径,然后从Mac OS终端进行检查。