尝试JSONSerialization时崩溃

时间:2018-08-24 15:04:47

标签: get crash swift4

我正在处理一个小项目,该项目从GET请求接收信息。 有一个json响应,我正在尝试从中获取信息。我发生了一次奇怪的崩溃。 这是代码:

    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
        guard let data = data, error == nil else { return }

        do {
            let json = String(data: data, encoding: String.Encoding.utf8)
            let jsonString:String = json!
            print(jsonString)
            let jsonObjects = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]
            // crash here 
            let revenue = jsonObjects["revenue"] as? Float ?? 404
            print("revenue: \(revenue)")

        } catch let error as NSError {
            print("error:\(error)")
        }

并且日志显示此错误:

 Could not cast value of type '__NSSingleObjectArrayI' (0x10e567528) to      'NSDictionary' (0x10e5681a8).
2018-08-24 15:02:42.750890+0300 creatingp[18743:1290643] Could not cast     value of type '__NSSingleObjectArrayI' (0x10e567528) to 'NSDictionary'     (0x10e5681a8).

谁能告诉我如何解决这个问题?

0 个答案:

没有答案