gpt4 book ai didi

swift - 无法使用类型的参数列表调用 'jsonObject'

转载 作者:行者123 更新时间:2023-11-28 12:23:49 25 4
gpt4 key购买 nike

当使用此代码从具有基本身份验证的服务器获取 JSON 时:

                    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)")
}

我收到以下错误

Cannot invoke 'jsonObject' with an argument list of type '(with: Data, options: [Any], error: [Any])'

最佳答案

查看 documentation , 看来你只有两个选择:

jsonObject(with: Data, options: JSONSerialization.ReadingOptions = [])

jsonObject(with: InputStream, options: JSONSerialization.ReadingOptions = [])

我认为您正在寻找第一种方法。也许你把它和

混淆了
writeJSONObject(Any, to: OutputStream, options: JSONSerialization.WritingOptions = [], error: NSErrorPointer)

所以,简而言之,这样的方法是不存在的。这就是您收到错误的原因。

关于swift - 无法使用类型的参数列表调用 'jsonObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com