DataWithContentsOfMappedFile deprecated in iOS 8.0

I am using the following code to extract data from a JSON file.

let jsonData = try NSData.dataWithContentsOfMappedFile("/Users/User/Desktop/Employee.json")

I get the following error

dataWithContentsOfMappedFile was deprecated in iOS 8.0: Use +dataWithContentsOfURL:options:error: and NSDataReadingMappedIfSafe or NSDataReadingMappedAlways instead

Can someone please let me know the alternative?

+4
source share
1 answer

try it

to extract from local path use contentsOfFileelse usecontentsOfURL

 let contents: NSData?
    do {
        contents = try NSData(contentsOfFile: "/Users/User/Desktop/Employee.json", options: NSDataReadingOptions.DataReadingMappedAlways)
    } catch _ {
        contents = nil
    }

    print(contents)

if you want to access only contentsOfFileuse the following line

    let contents   =  NSData(contentsOfFile:"/Users/User/Desktop/Employee.json")
     print(contents)
+2
source

Source: https://habr.com/ru/post/1615697/


All Articles