NSData在版本9.0中给出了一个错误

时间:2017-03-06 06:15:15

标签: ios swift swift3 swift2 apple-developer

下面给出的是我在swift中编写的函数,这个代码在10.0版本的iPhone中工作正常,但在9.0中出现错误

  

CFURLCopyResourcePropertyForKey失败,因为它传递了一个URL   没有方案无法加载:文件   “Recording2017-03-06_11.08.53000.mp3”无法打开。

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)")
        }
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

您可以使用NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];之类的内容您传入的字符串不是有效的网址,它只是一个文件路径。要使其成为URL,您需要添加file://

的方案