dashbashrumble
dashbashrumble

Reputation: 251

Swift 2.1: Unable to map JSON object to Realm object using ObjectMapper

I am trying to map my JSON object to a Realm object using ObjectMapper, but I keep getting nil.

import RealmSwift
import ObjectMapper

class Notification: Object, Mappable {
dynamic var id = 0
dynamic var isProcessed = false
dynamic var type = ""
var supersedes : Int?

required convenience init?(_ map: Map) {
    self.init()
}

// Mappable
func mapping(map: Map) {
    id <- map["id"]
    isProcessed <- map["isProcessed"]
    type <- map["type"]
    supersedes <- map["supersedes"]
}
}

I am using the following line of code to map the incoming JSON to the above Realm object.

let notif = Mapper<Notification>().map(notification) // notif here is nil

notification is a JSON object (SwiftyJSON library used)

Sample Notification JSON:

  {
        data =             {
            buyerInfo =                 {
                image = "";
                name = "";
                userId = UID2268351488;
            };
            sellerSKUs =                 (
                                    {
                    id = SSK236123228808967424;
                    price =                         {
                        amount = 888;
                        currency = THB;
                    };
                    quantity = 2;
                },
                                    {
                    id = SSK563365068895040768;
                    price =                         {
                        amount = 6865;
                        currency = THB;
                    };
                    quantity = 1;
                }
            );
            subOrderId = SOD751798094080240;
        };
        id = 39038;
        isProcessed = 0;
        supersedes = "<null>";
        type = PendingSubOrderConfirmationNotification;
    },

Please help!

Upvotes: 1

Views: 640

Answers (1)

marius
marius

Reputation: 7806

You can't combine ObjectMapper and SwiftyJSON in that way. Either you provide the rawValue from the SwityJSON JSON struct as argument to ObjectMapper's Mapper.map or you use instead just NSJSONSerialization.

Upvotes: 1

Related Questions