I'm using ObjectMapper with Realm with these version.
But when added new class with Realm + ObjectMapper, an error has occured.
below line is error message.
Must call a designated initializer of the superclass 'Navigator'
and thisi is new class i added.
import RealmSwift
import ObjectMapper
class Navigator: Object, Mappable {
dynamic var id : Int = 0
dynamic var name : String = ""
dynamic var imageURL : String = ""
dynamic var coverImageURL : String = ""
dynamic var totalPlayCount : Int = 0
dynamic var totalFavCount : Int = 0
dynamic var createdAt : NSDate = NSDate()
dynamic var updatedAt : NSDate = NSDate()
var guides : [Guide] {
return linkingObjects(Guide.self, forProperty: "navigator")
}
override class func primaryKey() -> String {
return "id"
}
override static func indexedProperties() -> [String] {
return ["name", "imageURL", "totalPlayCount", "totalFavCount"]
}
// Mark: - ObjectMapper
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
imageURL <- map["avatar_url"]
coverImageURL <- map["cover_url"]
totalPlayCount <- map["listen_count"]
totalFavCount <- map["favorite_count"]
createdAt <- (map["created_at"], ISO8601DateTransform())
updatedAt <- (map["updated_at"], ISO8601DateTransform())
}
}
this class works correctly.
import RealmSwift
import ObjectMapper
class Guide: Object, Mappable {
dynamic var id : Int = 0
dynamic var title : String = ""
dynamic var spotName : String = ""
dynamic var placeId : String = ""
dynamic var explanation : String = ""
dynamic var category : String = ""
dynamic var totalPlayCount : Int = 0
dynamic var totalFavCount : Int = 0
dynamic var latitude : Double = 0.0
dynamic var longitude : Double = 0.0
dynamic var createdAt : NSDate = NSDate()
dynamic var updatedAt : NSDate = NSDate()
dynamic var navigator: Navigator?
var relatedGuides = List<Guide>()
override class func primaryKey() -> String {
return "id"
}
// 銈ゃ兂銉囥儍銈偣瑷畾
override static func indexedProperties() -> [String] {
return ["name", "category", "firstImageUrl", "totalPlayCount"]
}
// override static func ignoredProperties() -> [String] {
// return []
// }
// Mark: - ObjectMapper
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
title <- map["name"]
spotName <- map["spot.name"]
explanation <- map["description"]
category <- map["category.name"]
totalPlayCount <- map["listen_count"]
totalFavCount <- map["favorite_count"]
latitude <- map["geometry.latitude"]
longitude <- map["geometry.longitude"]
createdAt <- (map["created_at"], ISO8601DateTransform())
updatedAt <- (map["updated_at"], ISO8601DateTransform())
}
}
so i'm confused what to do next.
someone please give me advices.
Hi there, I just tested this code and I am not getting any errors. Are you still having this problem?
I have exactly the same problem:
import ObjectMapper
import RealmSwift
class WRUser: Object, Mappable {
dynamic var ID: Int = 0
dynamic var email: String = ""
// MARK: - Init
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
ID <- map["user.id"]
email <- map["user.email"]
}
}
Actually building a project gives an error: Must call a designated initializer of the superclass "WRUser"
Any thoughts?
UPDATE:
The problem occurs only if I create a subclass of WRUser, say WRUserClient. As long as WRUser is a class that has no subclasses, the project compiles.
@nashirox did you subclass?
Any idea why does this happen?
Having same issue with a subclass of another mappable object. This issue says closed but there does not appear to be a solution?
Hi,
I also having same error when subclassing mappable object.
The working solution for me is by adding init method (same as parent class)
So, in above case :
class WRUserSubclass: WRUser {
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
// extended mapping here
}
}
Hope this help
Most helpful comment
Hi,
I also having same error when subclassing mappable object.
The working solution for me is by adding init method (same as parent class)
So, in above case :
Hope this help