Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm Object JSONDictionary method not working in Swift #74

Open
marksbren opened this issue Dec 10, 2015 · 1 comment
Open

Realm Object JSONDictionary method not working in Swift #74

marksbren opened this issue Dec 10, 2015 · 1 comment

Comments

@marksbren
Copy link

I have been trying to implement this on my current Swift project, but cannot get it working. Here is my model:

//
//  RealmComment.swift
//  phefeed
//
//  Created by Mark Brenneman on 11/25/15.
//  Copyright © 2015 Mark Brenneman. All rights reserved.
//

import Foundation
import RealmSwift

class RealmComment: Object {
    dynamic var text: String?
    dynamic var id: String?
    dynamic var createdAt: Double = 0.00
    dynamic var updatedAt: Double = 0.00
    dynamic var user: RealmUser?
    dynamic var post: RealmPost?

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

    func JSONOutboundMappingDictionary() -> NSDictionary {
        return [
            "id": "objectId",
            "createdAt": "createdAt",
            "updatedAt": "updatedAt",
            "user":"user",
            "post":"post",
            "text":"text"
        ]
    }

    func JSONInboundMappingDictionary() -> NSDictionary {
        return [
            "id": "objectId",
            "createdAt": "createdAt",
            "updatedAt": "updatedAt",
            "user":"user",
            "post":"post",
            "text":"text"
        ]
    }    
}

I am then trying to convert an object to JSON Dictionary using this (to convert a comment from one RealmComment (Realm+JSON) to an MBComment (ObjectMapper)

    class func realmCommentToMbComment(realmComment: RealmComment) -> MBComment {
        var newMbComment = MBComment()
        let realm = try! Realm()
        try! realm.write {

            let JSONString = realmComment.JSONDictionary
            newMbComment = Mapper<MBComment>().map(JSONString)! //this is using ObjectMapper

        }
        return newMbComment

    }

However, this will not compile and gives me an error "Value of type RealmComment has no member JSONDictionary".

@viktorasl
Copy link
Contributor

I guess the problem is in class RealmComment: Object. You should be subclassing RLMObject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants