想从本地一个json文件取数据,单数照着使用这个方法一直报错误:
Cannot invoke ‘jsonObject’ with an argument list of type ‘(with: NSData,options: JSONSerialization.ReadingOptions)’
let path = Bundle.main.path(forResource: "baseInfo",ofType: "json")
let data = NSData(contentsOfFile: path!)
let jsonOb = try! JSONSerialization.jsonObject(with: data,options: JSONSerialization.ReadingOptions.mutableContainers)
这里竟然是因为一个参数的类型出了问题,纠结老半天,
let jsonOb = try! JSONSerialization.jsonObject(with: data! as Data,options: JSONSerialization.ReadingOptions.mutableContainers)
因为这里需要的参数类型是Data
而不是NSData
,习惯性的以为是一样的,习惯害人。。。