gpt4 book ai didi

swift - AlamofireObjectMapper : JSON with Identifier

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

封装数据时是否需要为AlamofireObjectMapper编写一个wrapper-object?例如。如果天气数据位于命名数组“data”内。映射的最佳解决方案是什么?

{
"data": [
{
"conditions": "Partly cloudy",
"day": "Monday",
"temperature": 20
},
{
"conditions": "Showers",
"day": "Tuesday",
"temperature": 22
},
{
"conditions": "Sunny",
"day": "Wednesday",
"temperature": 28
}
]
}

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
Alamofire.request(.GET, URL, parameters: nil)
.responseArray { (response: [Forecast]?, error: ErrorType?) in
if let response = response {
for forecast in response {
print(forecast.day)
print(forecast.temperature)
}
}
}

最佳答案

事实上,这是最舒适的选择。

你可以有一个包装对象,例如:

class Forecast: Mappable {

// MARK: - Attributes

var conditions: String = ""
var day: String = ""
var temperature: NSNumber

// MARK: - Methods

init() { }

required init?(_ map: Map) { }

func mapping(map: Map) {
conditions <- map["conditions"]
day <- map["day"]
temperature <- map["temperature"]
}
}

然后,管理请求:

Alamofire.request(.POST, URL).responseObject({ (response: Response<Forecast, NSError>) -> Void in
if response.result.isSuccess {
print(response.result.value!)
}
})

祝你好运!

关于swift - AlamofireObjectMapper : JSON with Identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33481133/

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