I am using ObjectMapper for JSON parsing and Realm for persistence please help me how can I declare a dictionary so that it will comptiable with both Object Mapper and Realm.
class UserInfo: Object,Mappable
{
var name:String?
var identifier:String?
var accountType:String?
var devices:[UserDevice]?
required init?(map: Map)
{
}
// Mappable
func mapping(map: Map)
{
name <- map["username"]
identifier <- map["identifier"]
accountType <- map["accountType"]
devices <- map["devices"]
}
}
==========
class UserDevice:Object,Mappable
{
var deviceName:String?
var deviceType:String?
var deviceIdentifier:String?
required init?(map: Map)
{
}
// Mappable
func mapping(map: Map) {
deviceName <- map["name"]
deviceType <- map["type"]
deviceIdentifier <- map["identifier"]
}
}
JSON :
{
username = santhosh;
"identifier" = "IDJSDJSJS";
configstatus = SET;
configtype = DEFAULT;
"data_center" = "evs.idrive.com";
devices = {
"Ankita\LKKKK" = {
identifier = LJJDFDD;
name = "Ankita iPAD";
type = iPad;
};
"ARUN\LKKKHJH" = {
identifier = LJJDFDFFD;
name = "ARUN iPhone";
type = ipHone;
};
accountStatus = 2;
acctype = Test;
}
How can I declare user devices so that it will be compatible with both Realm and ObjectMapper.
There is an ObjectMapper extension for Realm that allows you to handle Realm
List
properties.In your code above, you're not defining your properties as ones Realm will recognize. You need to prefix
dynamic
for all primitive types and use the RealmList
object for the array.The ObjectMapper extension has sample code to show how to parse the
List
object, but it should look something like this: