无法使用类型的参数列表调用“jsonObject" [英] Cannot invoke 'jsonObject' with an argument list of type
本文介绍了无法使用类型的参数列表调用“jsonObject"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用此代码从具有基本身份验证的服务器获取 JSON 时:
When using this code to get JSON from a server with Basic auth:
let config = URLSessionConfiguration.default
let userPasswordData = "\(username!):\(password!)".data(using: .utf8)
let base64EncodedCredential = userPasswordData!.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
let authString = "Basic \(base64EncodedCredential)"
config.httpAdditionalHeaders = ["Authorization" : authString]
let session = URLSession(configuration: config)
let url = URL(string: "https://theforest.academy/api/v1/auth")
let task = session.dataTask(with: url!) {
(data, response, error) in
if (response as? HTTPURLResponse) != nil {
do {
if let data = data {
// let json1 = try JSONSerialization.jsonObject(with: data) as? [String: Any]
let json = try JSONSerialization.jsonObject(with: data, options: [], error: []) as? [String: Any]
if(json["valid"]) {
print(json["token"])
} else {
print("Invalid login")
}
}
} catch {
print("Error deserializing JSON: \(error)")
}
我收到以下错误
无法使用类型为(具有:数据,选项:[Any],错误:[Any])'
Cannot invoke 'jsonObject' with an argument list of type '(with: Data, options: [Any], error: [Any])'
推荐答案
查看文档,看来你只有两个选择:
Looking at the documentation, it seems that you only have two choices:
jsonObject(with: Data, options: JSONSerialization.ReadingOptions = [])
和
jsonObject(with: InputStream, options: JSONSerialization.ReadingOptions = [])
我认为您正在寻找第一种方法.也许你把它与
I think you are looking for the first method. Perhaps you are confusing it with
writeJSONObject(Any, to: OutputStream, options: JSONSerialization.WritingOptions = [], error: NSErrorPointer)
所以,总之,这样的方法是不存在的.这就是您收到错误的原因.
So, in short, such a method does not exist. That is why you are getting the error.
这篇关于无法使用类型的参数列表调用“jsonObject"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文