ObjectMapper无法序列化响应

Aak*_*ash 7 ios swift alamofire objectmapper

我正在使用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)           
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

这是我的DataModel类

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"]
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试添加空白参数,因为这个api不需要参数,也添加了默认的URl编码但没有帮助.

我不知道我在哪里遗漏了什么,这个代码在api响应中没有任何null时工作正常.请帮忙!!

谢谢

小智 2

您的代码没有问题,但 URL 中的 JSON 格式错误。第三个物体的温度场是空的。