2

hi I am trying to convert json response from a web service to realm object and insert it into realm database using object mapper but returns objects with null values

Alamofire.request(url).responseJSON { response in
   let products = Mapper<ProRealm>().map(JSONObject:response.result.value)
        print("products",products) // object with null values
   }

My ProRealm class is

class ProRealm: Object, Mappable {

dynamic var additives_count: String?
var rating: String?
var updated: Bool = false;
var name: String?
var barcode: String?
var product_key: String?
var hazard_count: String?
var state: String?
var no_of_users_rated: String?
var thumbnail: String?
var overall_rating : String?
var is_food_or_beverage : Bool = false

//Impl. of Mappable protocol
required convenience init?(map: Map) {
    self.init()
}

func mapping(map: Map) {
    additives_count    <- map["additives_count"]
    rating <- map["rating"]
    updated <- map["updated"]
    name <- map["name"]
    barcode <- map["barcode"]
    product_key <- map["product_key"]
    hazard_count    <- map["hazard_count"]
    state <- map["state"]
    no_of_users_rated <- map["no_of_users_rated"]
    thumbnail <- map["thumbnail"]
    overall_rating <- map["overall_rating"]
    is_food_or_beverage <- map["is_food_or_beverage"]
}
}

In Json response, the products are inside the products key. please advice how to convert it into realm object

["kind": aWareInternalAPI#productsItem, "products":(
{
"additives_count" = 3;
barcode = 12345;
"hazard_count" = 0;
"is_food_or_beverage" = 1;
name = "Water Bottle";
"no_of_users_rated" = 1;
"overall_rating" = "0.0";
"product_key" = "ahdzfmF3YXJlLWJhY2tlbmQtc3RhZ2luZ3IlCxIEVXNlchiAgICgvoOTCQwLEgdQcm9kdWN0GICAgIDE_JAKDA";
rating = 0;
state = "OCR_PROCESSING";
thumbnail = "http://lh3.googleusercontent.com/dMxwgSQB02osZJJex4S57iupaMT9tjDYZaD7mweJUjYmI1KNEcZZe1syBwrRs1GbYdZNrRUtQwRYUwXiAEscGNYH-J9f3gJOXYO1rQ=s150";
updated = 1;
},
{
"additives_count" = 0;
barcode = 53647825898248485;
"hazard_count" = 0;
"is_food_or_beverage" = 1;
"no_of_users_rated" = 0;
"overall_rating" = "0.0";
"product_key" = ahdzfmF3YXJlLWJhY2tlbmQtc3RhZ2luZ3IlCxIEVXNlchiAgICgvoOTCQwLEgdQcm9kdWN0GICAgICumYAJDA;
rating = "";
state = "OCR_PROCESSING";
thumbnail = "http://lh3.googleusercontent.com/0D55ZXkG8Ua5ULDK69Po-IHeDPIfXZHOi7LlLURoc1qZzmNst57xUMQSPzWTW5miSDglc5wKDA4QlvLvnD6aMOqIHcwlj_HY-Hs=s150";
updated = 1;
})]

Please advice

Mohanraj
  • 587
  • 4
  • 21

1 Answers1

2

All your properties except updated and is_food_or_beverage are nilable, so there is a possibility that they might be nil. If you know that they should have a value and you´re still getting nil make sure to check the keys in your mapping function.

Update:
I´m not 100% sure of how this works with the syntax with Mappable, but you need to do it something like this:

class Products: Object, Mappable {
    let products: [Product]?

    //Impl. of Mappable protocol
    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        products    <- map["products"]
    }
}

class Product: Object, Mappable {
    dynamic var additives_count: String?
    var rating: String?
    var updated: Bool = false;
    var name: String?
    var barcode: String?
    var product_key: String?
    var hazard_count: String?
    var state: String?
    var no_of_users_rated: String?
    var thumbnail: String?
    var overall_rating : String?
    var is_food_or_beverage : Bool = false

    //Impl. of Mappable protocol
    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        additives_count    <- map["additives_count"]
        rating <- map["rating"]
        updated <- map["updated"]
        name <- map["name"]
        barcode <- map["barcode"]
        product_key <- map["product_key"]
        hazard_count    <- map["hazard_count"]
        state <- map["state"]
        no_of_users_rated <- map["no_of_users_rated"]
        thumbnail <- map["thumbnail"]
        overall_rating <- map["overall_rating"]
        is_food_or_beverage <- map["is_food_or_beverage"]
    }
}

So create two different classes one that holds an array of products and the other is a Product.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107