gpt4 book ai didi

ios - 更正可编码结构 Swift 4 和 KeyNotFound 错误

转载 作者:行者123 更新时间:2023-11-28 06:01:52 25 4
gpt4 key购买 nike

已编辑

您好,我是第一次使用可编码协议(protocol):我的 JSON 响应(使用 POSTMAN)是:

[
{
"FltId": "928",
"Carrier": "AS",
"Orig": "ANC",
"Dest": "SEA",
"CutOffTime": "40",
"FltDirection": 0,
"SchedDepTime": "2018-03-21T01:25:00",
"EstDepTime": "2018-03-21T01:25:00",
"SchedArrTime": "2018-03-21T05:43:00",
"EstArrTime": "2018-03-21T05:43:00",
"ActualTime": "",
"OrigZuluOffset": "-8",
"DestZuluOffset": "-7",
"DestGate": "C15",
"OrigGate": "",
"CodeShares": [
{
"FltId": "",
"Carrier": ""
}
],
"TailId": "251",
"FleetType": "737-900R",
"Status": "ON TIME"
},
{
"FltId": "3422",
"Carrier": "OO",
"Orig": "BLI",
"Dest": "SEA",
"CutOffTime": "40",
"FltDirection": 0,
"SchedDepTime": "2018-03-21T05:00:00",
"EstDepTime": "2018-03-21T05:00:00",
"SchedArrTime": "2018-03-21T05:56:00",
"EstArrTime": "2018-03-21T05:56:00",
"ActualTime": "",
"OrigZuluOffset": "-7",
"DestZuluOffset": "-7",
"DestGate": "C16",
"OrigGate": "",
"CodeShares": [
{
"FltId": "",
"Carrier": ""
}
],
"TailId": "417",
"FleetType": "CRJ-200",
"Status": "ON TIME"
}]

我在我的 swift 代码中创建了一个可编码的对象结构,想知道它是否正确:

struct Response: Codable {
let flights: [Flight]
}

struct Flight: Codable{
let FltId, Carrier, Orig, Dest, CutOffTime, FltDirection, SchedDepTime, EstDepTime, SchedArrTime, EstArrTime, ActualTime, OrigZuluOffset, DestZuluOffset, DestGate, OrigGate, TailId, FleetType, Status: String, CodeShares:[CodeShare]
}
struct CodeShare: Codable {
let FltId, Carrier: String
}

这是否可以进一步修复或改进?此外什么是编码键?我收到这个奇怪的错误:

失败(Swift.DecodingError.keyNotFound(table_view_api.FlightWebService.Response。(_954486EAAC5BB7DDCEFC26E493D69F38 中的 CodingKeys)。航类,Swift.DecodingError.Context(编码路径:[基础。(_12768CA107A31EF2DCE034FD7 中的 _JSONKey)(字符串 541FD7 中的 _JSONKey) ", intValue: Optional(0))], debugDescription: "No value associated with key flights (\"flights\").", underlyingError: nil)))

我错过了什么吗?

此外,我正在使用正确的身份验证 header 发出我的网络服务请求,例如:

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("Basic YWFnZTQxNDAxMjgwODYyNDk3NWFiYWNhZjlhNjZjMDRlMWY6ODYyYTk0NTFhYjliNGY1M2EwZWJiOWI2ZWQ1ZjYwOGM=", forHTTPHeaderField: "Authorization")



func getFlightData(for airportCode: String, minutesBehind:String, minutesAhead:String, completion: ((Result<[Response]>) -> Void)?) {
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "api.qa.alaskaair.com"
urlComponents.path = "/1/airports/"+airportCode+"/flights/flightInfo"
urlComponents.queryItems = [
URLQueryItem(name: "city", value: airportCode),
URLQueryItem(name: "minutesBehind", value: minutesBehind),
URLQueryItem(name: "minutesAhead", value: minutesAhead)

]

guard let url = urlComponents.url else { fatalError("Could not create URL from components") }

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("Basic YWFnZTQxNDAxMjgwODYyNDk3NWFiYWNhZjlhNjZjMDRlMWY6ODYyYTk0NTFhYjliNGY1M2EwZWJiOWI2ZWQ1ZjYwOGM=", forHTTPHeaderField: "Authorization")

let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request) { (responseData, response, responseError) in
DispatchQueue.main.async {
if let error = responseError {
completion?(.failure(error))
} else if let jsonData = responseData {

let decoder = JSONDecoder()

do {

let posts = try decoder.decode(Array<Flight>.self, from: jsonData)
completion?(.success(posts))
} catch {
completion?(.failure(error))
}
} else {
let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Data was not retrieved from request"]) as Error
completion?(.failure(error))
}
}
}

task.resume()
}

这会有问题吗?

最佳答案

听起来您正在尝试将 JSON 解码为响应。那是你的错误。鉴于您定义的结构和您显示的 JSON,您的 JSON 显然是(或打算是)航类数组,也就是说,Array<Flight>.self .这就是您需要解码的内容。

您的 Response 结构实际上显然与 JSON 无关;您不能解码为响应,因为 JSON 不是带有键 "flights" 的字典.您可以手动创建一个 Response 并设置它的 flights 手动到那个结果,但是你的 Response 结构,虽然它可能在存储方面派上用场,但与你的 JSON 无关。

关于ios - 更正可编码结构 Swift 4 和 KeyNotFound 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49411597/

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