您好我正在尝试将json响应从Web服务转换为realm对象,并使用对象映射器将其插入到realm数据库中,但返回具有空值的对象
Alamofire.request(url).responseJSON { response in
let products = Mapper<ProRealm>().map(JSONObject:response.result.value)
print("products",products) // object with null values
}
我的ProRealm课程是
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"]
}
}
在Json的回复中,产品都在产品密钥内。请建议如何将其转换为领域对象
["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;
})]
请咨询
答案 0 :(得分:2)
除updated
和is_food_or_beverage
之外的所有属性均为nilable
,因此它们可能是nil
。如果你知道他们应该有一个值,你仍然无法确保检查映射函数中的键。
<强>更新强>
我不是100%肯定这与Mappable
的语法一起使用,但你需要这样做:
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"]
}
}
因此,创建两个不同的类,一个包含products
数组,另一个包含Product
。