gpt4 book ai didi

swift - 可编码数组和 Alamofire 的问题

转载 作者:行者123 更新时间:2023-11-30 10:28:03 25 4
gpt4 key购买 nike

我尝试通过 Alamofire 提交以下请求,但收到以下错误:

2020-01-13 09:41:05.912103-0600 AFNetworkingDemo[29720:604258] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'

我的假设是,这是我在对象内定义数组的方式(我遵循了此处找到的一些 Material :https://benscheirman.com/2017/06/swift-json)

struct ProgramRequest: Codable {
var userID: Int
var programData: ProgramData

var json: Constants.Json {
return [
"userID": userID,
"programData": programData.json,
]
}
}

struct ProgramData: Codable {
var airhotel: [AirHotel]
var security: [Security]

var json: Constants.Json {
return [
"airhotel": airhotel,
"security": security
]
}
}

struct AirHotel: Codable {
var id: Int
var loyaltyNumber: String

var json: Constants.Json {
return [
"id": id,
"loyaltyNumber": loyaltyNumber
]
}
}

struct Security: Codable {
var vendorCode: String
var loyaltyNumber: String

var json: [String: Any] {
return [
"vendorCode": vendorCode,
"loyaltyNumber": loyaltyNumber
]
}
}

每个级别的 json 字典都是为 Alamofire 适本地渲染对象。对于给定的示例,如果我使用以下命令将其打印出来:

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted

let jsonData = try jsonEncoder.encode(programRequest)

if let jsonString = String(data: jsonData, encoding: .utf8) {
print(jsonString) #1
}

print("IsValidJSON: ", JSONSerialization.isValidJSONObject(programRequest)) #2

print(programRequest.json) #3

urlRequest = try! JSONEncoding.default.encode(urlRequest, with: programRequest.json) #4 causes error

1 个输出:

{
"userID" : 10021,
"programData" : {
"airhotel" : [],
"security" : [
{
"vendorCode" : "sty",
"loyaltyNumber" : "Loyal1"
}
]
}
}

2个输出 IsValidJSON:假

3 输出 - 我注意到输出中的 AFNetworkingDemo.Security 就是 Alamofire JSONEncoding 的内容:

["programData": ["security": [AFNetworkingDemo.Security(vendorCode: "sty", loyaltyNumber: "Loyal1")], "airhotel": []], "userID": 10021]

我的问题是,我需要对 ProgramRequest 的 AirHotel 和安全部分进行哪些更改才能解决我的 Alamofire 问题?

最佳答案

第 1 步:停止重新发明 Codable

不需要并且错误地使用了 json 计算属性。

删除它。

struct ProgramRequest: Codable {
var userID: Int
var programData: ProgramData
}

struct ProgramData: Codable {
var airhotel: [AirHotel]
var security: [Security]
}

struct AirHotel: Codable {
var id: Int
var loyaltyNumber: String
}

struct Security: Codable {
var vendorCode: String
var loyaltyNumber: String
}

第 2 步:开始使用 Codable

现在给定一个programRequest,例如。像这样

let programRequest = ProgramRequest(userID: 1, programData: ProgramData(airhotel: [AirHotel(id: 1, loyaltyNumber: "loyaltyNumber")], security: [Security(vendorCode: "loyaltyNumber", loyaltyNumber: "loyaltyNumber")]))

您只需编写即可生成表示 JSON 的正确数据值

let data = try JSONEncoder().encode(programRequest)

并且(如果您愿意)您可以将数据转换为字符串

let json = String(data: data, encoding: .utf8)

输出

Optional("{\"userID\":1,\"programData\":{\"airhotel\":[{\"id\":1,\"loyaltyNumber\":\"loyaltyNumber\"}],\"security\":[{\"vendorCode\":\"loyaltyNumber\",\"loyaltyNumber\":\"loyaltyNumber\"}]}}")

就是这样

关于swift - 可编码数组和 Alamofire 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720432/

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