NSData 在 9.0 版中给我一个错误

NSData gives me an error in version 9.0

下面是我用 swift 编写的函数 此代码在 iPhone 的 10.0 版本中运行良好,但在 9.0 saying

CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme Failed to load: The file “Recording2017-03-06_11.08.53000.mp3” couldn’t be opened.

partFileURL 将具有

/private/var/mobile/Containers/Data/Application/B8F9055D-D816-4E27-BA2A-B13F0EE97709/tmp/Recording2017-03-06_11.08.53000.mp3

这是我的以下功能-:

//function
func putPartUplaod(partFileURL:URL , partFileNumber:Int)
{
        var partfileData:Data?
        var md5hash:Any?
        var sha256hash:Any?
        //  var error: NSError?   
        let uri:URL = NSURL(fileURLWithPath: partFileURL.absoluteString) as URL

        //Get MD5 Digest
        do{
            print(partFileURL)
            partfileData = try NSData(contentsOf: uri, options: NSData.ReadingOptions.dataReadingMapped) as Data    
            print("hello" , partfileData);
            md5hash = partfileData?.md5().base64EncodedString()  
        }catch let error as NSError {
            print("Failed to load: \(error.localizedDescription)")
        }
}

/private/var/mobile/Containers/Data/Application/B8F9055D-D816-4E27-BA2A-B13F0EE97709/tmp/Recording2017-03-06_11.08.53000.mp3 是文件路径而不是 url。 url 有一个方案(或协议)。查看您的错误消息。用

创建你的url
URL(fileURLWithPath:"/private/var/mobile/Containers/Data/Application/B8F9055D-D816-4E27-BA2A-B13F0EE97709/tmp/Recording2017-03-06_11.08.53000.mp3")

您可以使用 NSData *data = [[NSFileManager defaultManager] contentsAtPath:path]; 您传递的字符串无效 URL,它只是一个文件路径。要使其成为 URL,您需要添加方案 file://.