Realm-JSON
Realm-JSON copied to clipboard
Realm Object JSONDictionary method not working in Swift
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".
I guess the problem is in class RealmComment: Object. You should be subclassing RLMObject