我使用以下代码从
JSON文件中检索数据.
let jsonData = try NSData.dataWithContentsOfMappedFile("/Users/User/Desktop/Employee.json")
我收到以下错误
dataWithContentsOfMappedFile was deprecated in iOS 8.0: Use +dataWithContentsOfURL:options:error: and NSDataReadingMappedIfSafe or NSDataReadingMappedAlways instead
有人可以让我知道替代方案吗?
解决方法
试试这个
从本地路径获取使用contentsOfFile否则使用contentsOfURL
let contents: NSData? do { contents = try NSData(contentsOfFile: "/Users/User/Desktop/Employee.json",options: NSDataReadingOptions.DataReadingMappedAlways) } catch _ { contents = nil } print(contents)
如果您只想访问contentsOfFile,请使用以下行
let contents = NSData(contentsOfFile:"/Users/User/Desktop/Employee.json") print(contents)