无法使用带有以下类型的参数列表调用jsonObject :(任意),选项:[Any]

时间:2017-04-16 17:18:43

标签: ios swift xcode

我刚刚更新到swift 3.1和Xcode 8.3.1,我在更新之前遇到了一个错误。 我不知道如何解决的错误是尝试解析行try! JSONSerialization.jsonObject

中API的json数据

我描述了下面的代码

public extension User {

    public static var fake: Resource<[User]> = Resource(request: { (url) -> URLRequest in
        var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
        components.path = "/v2/fake-users"
        var request = URLRequest(url: components.url!)
        request.httpMethod = "GET"
        return request
    }) { (data) -> ([User]) in

        let json = try! JSONSerialization.jsonObject(with: data, options:[]) >>>>ERRROR>>>> Cannot invoke 'jsonObject' with an argument list of type '(with: (Any), options: [Any])'

        var code = json as? [String: Any]
        let error = code?["code"] as! String?
            if error == "NOT_AUTHORIZED_EXPIRED_TOKEN" || error == "INTERNAL_ERROR"{
                let sessionController = SessionController()
                sessionController.refreshToken(refreshToken: (SessionProvider().getUserToken()?.refreshToken)!) { (error) in
                    if let error = error {
                        print("Error trying to login refreshToken: \(error)")
                    } else {
                        print("login refreshToken successful")
                    }
                }
            }
            else {
                print("\n code: \(error)\n")

            }
        let unboxer = try Unboxer(data: data as! Data)
        return try unboxer.unbox(key: "hits")
    }

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

  

}){(strData) - &gt; <[用户])

let data = strData.data(using: String.Encoding.utf8, allowLossyConversion: false)!
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)