I am using AlamofireObjectMapper, whenever the response contains any null value, it gives an error,
"FAILURE: Error Domain=com.alamofireobjectmapper.error Code=2 "ObjectMapper failed to serialize response." UserInfo={NSLocalizedFailureReason=ObjectMapper failed to serialize response.}"
This is how I am requesting
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)
}
}
}
And this is my DataModel Class
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"]
}
}
I also tried adding blank parameters as this api requires no parameters and also added default URl encoding but no help.
I don't know where I am missing something, this code works fine when there is not any null in the api response. Please help!!
Thanks
Your code has no problem, but the JSON in the URL is malformed. The temperature field of the third object is empty.