func jsonDateCalling(){
//Json Serilizarions
let jsonUrl = URL(string: "http://assetlinkasia.no-ip.biz:8001/hf_tracker/api/history.php?accesskey=12345&Vehilce=1618&FromDate=2018-05-10 13:11&ToDate=2018-05-14 12:11")
if let url = jsonUrl{
let data = NSData(contentsOf: url)
if let data = data{
do{
let jsonObject = try JSONSerialization.jsonObject(with: data as Date, options: .allowFragments)
if let object = jsonObject as? [NSString: AnyObject]{
if let allDevices = object["data"] as? [[NSString: AnyObject]]{
print("Successfull")
self.tableArray = allDevices
self.searchTextTableview.reloadData()
}
}
}catch{
print("Error Eccurred")
}
}
}
}
这是json的作品,但我面对的是#34;无法转换类型' NSData'输入'日期'在胁迫中#34;我怎么解决我正在考虑日期参数
的这个错误答案 0 :(得分:1)
1-替换此
let jsonObject = try JSONSerialization.jsonObject(with: data as Date, options: .allowFragments)
与
let jsonObject = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments)
它是Data
对象而不是Date
2-不需要片段
let jsonObject = try JSONSerialization.jsonObject(with: data as Data, options: [])
if let object = jsonObject as? [NSString: Any]
3-这一行
let data = NSData(contentsOf: url)
阻止主线程考虑使用URLSession
或Alamofire