2016-09-19 23 views
4

をシリアル化に失敗した応答が任意のNULL値が含まれている時はいつでも、それはエラーを与え、ObjectMapperが、私は<a href="https://github.com/tristanhimmelman/AlamofireObjectMapper" rel="nofollow">AlamofireObjectMapper</a>を使用しています応答

「FAILURE:エラードメイン= com.alamofireobjectmapper.errorコード= 2」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)   
       } 
      } 
     } 

を要求していますか

これはある "のUserInfo = {NSLocalizedFailureReason = ObjectMapperは。応答をシリアル化に失敗した}" そして、これは私のDataModelクラス

です10
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レスポンス内の任意のヌルがない場合、私は何かが足りないのですどこ私は知らない

は、このコードは正常に動作します。助けてください!!

おかげ

答えて

2

あなたのコードは問題ありませんが、URLでJSONは不正な形式です。 3番目のオブジェクトの温度フィールドが空です。

+0

これは私がヌル応答を得ることができますし、その時に私のコードは、空の値を持つことはnullを交換する必要があるときにケースがあるかもしれないと、null値が応答であった場合には、応答をシリアル化しない理由私の質問です。 – Aakash

+0

null値をそれぞれのデフォルト値に置き換える方法があるかどうかを知りたいだけです。 – Aakash

+0

私はあなたを理解していますが、配列の3番目の要素の温度フィールドにjsonが不正な形式であり、null値ではありません。 – danbarbozza

関連する問題

 関連する問題