Objectmapper: Mapper().toJSON and Realm

Created on 22 Oct 2016  路  5Comments  路  Source: tristanhimmelman/ObjectMapper

Hello everyone!

I use Xcode 8 and Swift 3. When I tried to transform motel to JSON I got the error libc++abi.dylib: terminating with uncaught exception of type NSException.
I read this issue but it didn't help me. How can I fix it?

My model

class ChatRealmMessageModel: Object, Mappable {

    ///  for interlocator id
    dynamic var id = ""
    dynamic var date = ""
    /// This is text.
    dynamic var message = ""
    dynamic var imageData: Data? = nil
    dynamic var imageURL: String? = nil
    dynamic var senderID = ""
    dynamic var conversationID = ""
    dynamic var isIncoming = true
    dynamic var isNew = false
    // local paramers
    dynamic var messageStatus = MessageStatus.success.rawValue

    override static func primaryKey() -> String? {
        return "id"
    }

    func mapping(map: Map) {
        id <- map["id"]
        date <- map["date"]
        message <- map["message"]
        imageData <- map["imageData"]
        imageURL <- map["imageURL"]
        senderID <- map["senderID"]
        conversationID <- map["conversationID"]
        isIncoming <- map["isIncoming"]
        isNew <- map["isNew"]
        messageStatus <- map["messageStatus"]
    }

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

    enum MessageStatus: String {
        case sending, success, failed
    }
}

So I transform to JSON
let JSON = Mapper().toJSON(messageModel)

Most helpful comment

Hi @Sweefties ! Yes, I did. I show you my example

class RealmUserInfo: Object, Mappable {

    dynamic var id = ""
    dynamic var firstName = ""
    dynamic var lastName = ""
    dynamic var photoPath: String?

    override class func primaryKey() -> String? {
        return "id"
    }

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

    func mapping(map: Map) {
        if map.mappingType == .fromJSON {
            id <- map["id"]
            firstName <- map["firstName"]
            lastName <- map["lastName"]
            photoPath <- map["photoPath"]
        } else {
            id >>> map["id"]
            firstName >>> map["firstName"]
            lastName >>> map["lastName"]
            photoPath >>> map["photoPath"]
        }
    }

}

Tell whether this helped you or not.

All 5 comments

@alexsanderkhitev did you find one solution?

Hi @Sweefties ! Yes, I did. I show you my example

class RealmUserInfo: Object, Mappable {

    dynamic var id = ""
    dynamic var firstName = ""
    dynamic var lastName = ""
    dynamic var photoPath: String?

    override class func primaryKey() -> String? {
        return "id"
    }

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

    func mapping(map: Map) {
        if map.mappingType == .fromJSON {
            id <- map["id"]
            firstName <- map["firstName"]
            lastName <- map["lastName"]
            photoPath <- map["photoPath"]
        } else {
            id >>> map["id"]
            firstName >>> map["firstName"]
            lastName >>> map["lastName"]
            photoPath >>> map["photoPath"]
        }
    }

}

Tell whether this helped you or not.

@alexsanderkhitev you saved me...
Thanks for your solution.

This is so inconvenient.. I have 15 models in my project and on each it looks like I have duplicate code for just one simple operation switch. I know that the ref/val type must be declared in compile time. However, can we do better that ?
@tristanhimmelman do we have a better solution ?

Unfortunately I do not have a better solution. Suggestions are welcomed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

borut-t picture borut-t  路  4Comments

delbyze picture delbyze  路  3Comments

danyalaytekin picture danyalaytekin  路  4Comments

patchthecode picture patchthecode  路  3Comments

loryhuz picture loryhuz  路  4Comments