iOS NSData.write(toFile)在将zip写入cacheDirectory时返回false

iOS NSData.write(toFile)在将zip写入cacheDirectory时返回false,ios,xcode,ios11,xcode9-beta,Ios,Xcode,Ios11,Xcode9 Beta,我正在尝试下载一个zip文件,并在解压缩之前将其写入cacheDirectory。但是,write(toFile:)始终返回false: import Foundation func downloadFile(url: URL, completion: @escaping (Result<Data>) -> Void) { print("starting dqueue to download " + url.absoluteString) DispatchQu

我正在尝试下载一个zip文件,并在解压缩之前将其写入cacheDirectory。但是,write(toFile:)始终返回false:

import Foundation

func downloadFile(url: URL, completion: @escaping (Result<Data>) -> Void) {
    print("starting dqueue to download " + url.absoluteString)
    DispatchQueue.global(qos: .background).async {
        print("Starting download")
        let data = NSData.init(contentsOf: url)
        if data == nil {
            print("Data nil!")
        }

        // Find cache
        print("Got data!")
        let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
        let path = paths[0]

        let dataPath = URL(fileURLWithPath: path).appendingPathComponent("mod.zip")
        if (FileManager.default.fileExists(atPath: dataPath.absoluteString)) {
            print("File already exists!")
        }

        // Save data
        print("Saving to " + dataPath.absoluteString)
        if (data?.write(toFile: dataPath.absoluteString, atomically: true) ?? false) {
            if (FileManager.default.fileExists(atPath: dataPath.absoluteString)) {
                print("exists!")
            } else {
                print("does not exist!")
            }

            if (FileManager.default.isWritableFile(atPath: dataPath.absoluteString)) {
                print("Writable")
            }else {
                print("Not Writable")
            }

            DispatchQueue.main.async {
                print("This is run on the main queue, after the previous code in outer block")
            }
        } else {
            print("Failed to write to file!")
        }
    }
}
我试过:

  • 正在创建目录(它已存在)
  • 将原子更改为false(无影响)
  • 正在尝试查看权限(找不到此权限的任何文档)
  • 阅读并尝试以下问题:

我正在将Xcode v9.0 beta 2(9M137d)与IPad Air 2(iOS 11.0 15A5318g)配合使用

您需要使用
相对路径
而不是
绝对字符串

试试这个(我试过了,成功了):

<代码>导入基础 func下载文件(url:url,完成:@escaping(Result)->Void){ 打印(“开始dqueue下载”+url.absoluteString) DispatchQueue.global(qos:.background).async{ 打印(“开始下载”) let data=NSData.init(contentsOf:url) 如果数据==nil{ 打印(“数据为零!”) } //查找缓存 打印(“获取数据!”) 让path=NSSearchPathForDirectoriesInDomains(.cachesDirectory、.userDomainMask,true) 让路径=路径[0] 让dataPath=URL(fileURLWithPath:path)。appendingPathComponent(“mod.zip”) if(FileManager.default.fileExists(atPath:dataPath.relativePath)){ 打印(“文件已存在!”) } //保存数据 打印(“保存到”+dataPath.relativePath) if(data?write(toFile:dataPath.relativePath,原子性:true)??false){ if(FileManager.default.fileExists(atPath:dataPath.relativePath)){ 打印(“存在!”) }否则{ 打印(“不存在!”) } if(FileManager.default.isWritableFile(atPath:dataPath.relativePath)){ 打印(“可写”) }否则{ 打印(“不可写”) } DispatchQueue.main.async{ 打印(“这在主队列上运行,在外部块中的前一个代码之后”) } }否则{ 打印(“写入文件失败!”) } } }
请检查“let data=NSData.init(contentsOf:url)”,您的数据是否为零如果您从未实际检查是否收到数据,请尝试检查它是否为零更新的代码,数据是否为零。
starting dqueue to download https://github.com/minetest-mods/armor_monoid/archive/c7988a3dae32f2c1cb93fde31d9e53222a205eea.zip
Starting download
Got data!
Saving to file:///var/mobile/Containers/Data/Application/E734C574-1E94-44CB-9A6E-2AE1C8850E88/Library/Caches/mod.zip
Failed to write to file!
import Foundation

func downloadFile(url: URL, completion: @escaping (Result<Data>) -> Void) {
    print("starting dqueue to download " + url.absoluteString)
    DispatchQueue.global(qos: .background).async {
        print("Starting download")
        let data = NSData.init(contentsOf: url)
        if data == nil {
            print("Data nil!")
        }

        // Find cache
        print("Got data!")
        let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
        let path = paths[0]

        let dataPath = URL(fileURLWithPath: path).appendingPathComponent("mod.zip")
        if (FileManager.default.fileExists(atPath: dataPath.relativePath)) {
            print("File already exists!")
        }

        // Save data
        print("Saving to " + dataPath.relativePath)
        if (data?.write(toFile: dataPath.relativePath, atomically: true) ?? false) {
            if (FileManager.default.fileExists(atPath: dataPath.relativePath)) {
                print("exists!")
            } else {
                print("does not exist!")
            }

            if (FileManager.default.isWritableFile(atPath: dataPath.relativePath)) {
                print("Writable")
            }else {
                print("Not Writable")
            }

            DispatchQueue.main.async {
                print("This is run on the main queue, after the previous code in outer block")
            }
        } else {
            print("Failed to write to file!")
        }
    }
}