Godfather
Godfather

Reputation: 4340

fatal error: use of unimplemented initializer 'init(realm:schema:)' for class MyApp.User

Im getting this error when i try to load a user from the database.

    dispatch_async(dispatch_queue_create("background", nil)) {
        let realm = try! Realm()
        let users = realm.objects(User)
        print(users)
    }


class User: Object, Mappable {

  dynamic var id = 0
  dynamic var name = ""
  dynamic var userName = ""

required init() {
    super.init()
}

// MARK: Mappable
func mapping(map: Map) {
    id <- map["Id"]
    name <- map["Name"]
    userName <- map["UserName"]
}

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

}

I tried implemeting that init method but i get (Use of undeclared identifier RLMObjectSchema): enter image description here

Any hints?

Upvotes: 2

Views: 942

Answers (1)

YON
YON

Reputation: 1737

When I use realm, I will only use convenience init.

In your case

class User: Object, Mappable {

    dynamic var id = 0
    dynamic var name = ""
    dynamic var userName = ""

    // MARK: Mappable
    func mapping(map: Map) {
        id <- map["Id"]
        name <- map["Name"]
        userName <- map["UserName"]
    }

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

If you want implement designate init, you should implement

init(realm: RLMRealm, schema: RLMObjectSchema) {
    super.init(realm: realm, schema: schema)
}

When you implement designate init(), swift will not inherit other designate init methods which are required by realm.

Upvotes: 1

Related Questions