无法转换类型' NSData'的值输入'日期'在json工作中受到胁迫?

时间:2018-06-03 22:06:36

标签: swift

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;我怎么解决我正在考虑日期参数

的这个错误

1 个答案:

答案 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)

阻止主线程考虑使用URLSessionAlamofire