gpt4 book ai didi

ios - 使用 alamofire 发布带有字典和字典数组的 JSON 对象

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

我正在尝试发布一个像这样的对象:

{
"simulation" : { },
"simOptions" : [
{ },
{ }
]
}

并尝试通过调用我的SimulationRepository类来发布它:

SimulationsRepository().confirmSimulation(params: parameters) { (response) in
if let error = checkError(response) {
self.hideLoading()
self.showAlert(error)
return
}

guard let simsArray = SimulacaoArray(responseObject: response.result.value) else {
let error = response.error.debugDescription
self.hideLoading()
self.showAlert(error)
return
}

print(simsArray.simulation.count)
}

我已经尝试过这里的解决方案Sending json array via Alamofire在这里Alamofire: Send JSON with Array of Dictionaries ,但无法正确转换字典数组。

最佳答案

好吧,我已经通过映射模拟商品数组并将其转换为字典来解决,如下所示:

class SimulationsRepository: BaseRepository {

init() {
super.init(url: "/Simulations")
super.BaseUrl = "http://0.0.0.0:3000/api"
}

func confirmSimulation(simulation: Simulation, goods: [SimulatedGoods], then: @escaping ThenBlock) {
let goodsDict = goods.map { (simulatedGoods) -> [String: Any] in
return simulatedGoods.toDictionary()
}
let sim = simulation.toDictionary()

let params: [String: Any] = [
"simulation": sim,
"goods": goodsDict
]
super.customPost("/confirm", parameters: params, then: then)
}

}

顺便说一句,我从书中得到了这样映射数组的想法:https://www.hackingwithswift.com/store/pro-swift

关于ios - 使用 alamofire 发布带有字典和字典数组的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43076564/

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