【发布时间】:2017-01-27 09:08:07
【问题描述】:
我正在使用AlamofireObjectMapper,只要响应包含任何空值,就会出错,
"FAILURE: Error Domain=com.alamofireobjectmapper.error Code=2 "ObjectMapper 未能序列化响应。" UserInfo={NSLocalizedFailureReason=ObjectMapper 未能序列化响应。}"
这就是我的要求
let URL = "https://demo6336282.mockable.io/myapi"
Alamofire.request(URL).validate().responseObject { (response: DataResponse<WeatherResponse>) in
let weatherResponse = response.result.value
print(weatherResponse?.location)
if let threeDayForecast = weatherResponse?.threeDayForecast {
for forecast in threeDayForecast {
print(forecast.day)
print(forecast.temperature)
}
}
}
这是我的数据模型类
import Foundation
import ObjectMapper
import AlamofireObjectMapper
class WeatherResponse: Mappable {
var location: String? = ""
var threeDayForecast: [Forecast]? = []
required init?(map: Map){
}
func mapping(map: Map) {
location <- map["location"]
threeDayForecast <- map["three_day_forecast"]
}
}
class Forecast: Mappable {
var day: String? = ""
var temperature: Int? = 0
var conditions: String? = ""
required init?(map: Map){
}
func mapping(map: Map) {
day <- map["day"]
temperature <- map["temperature"]
conditions <- map["conditions"]
}
}
我也尝试添加空白参数,因为此 api 不需要参数,并且还添加了默认 URl 编码但没有帮助。
我不知道我在哪里遗漏了一些东西,当 api 响应中没有任何 null 时,此代码可以正常工作。请帮忙!!
谢谢
【问题讨论】:
标签: ios swift alamofire objectmapper