使用Alamofire将文件保存到.DocumentDirectory

时间:2015-03-28 22:14:24

标签: ios swift alamofire

我试图将文件下载并保存到Documents目录。我在客户端本地完成了此操作没有问题,但是当我在Alamofire中使用相同的路径时,我收到了一系列错误。

这是下载组件:

func downloadFileAtIndex(index: Int) {
    Alamofire.download(.GET, "\(serverURL)/user/\(userID)/project/\(arrayOfProjectIDs[index])/deliverable", { (temporaryURL, response) in
        let destinationFolder: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
        let filePath: NSURL = NSURL.fileURLWithPath("\(destinationFolder)/\(arrayOfProjectIDs[index])/myFile.png")!
        return filePath
        })
}

回复是:The operation couldn’t be completed. (Cocoa error 4.)

错误是:The operation couldn’t be completed. No such file or directory.

我知道该目录尚不存在,但我想创建它,这在本地保存时似乎不是问题。任何见解都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的目录不存在。您需要使用NSFileManager创建它。然后执行下载。