Guy
Guy

Reputation: 535

Use of unimplemented initializer 'init(realm:schema:)' when calling Realm().objects

I'm trying to retrieve a Realm's objects by using

Realm(path: Realm.defaultPath).objects(Fruits)

this what I get in result:

12: 7: fatal error: use of unimplemented initializer 'init(realm:schema:)' for class DB.Fruits

The object has only the following init:

required init() {
        super.init()
        nextPrimaryKey()
    }

I've gone through all the information about the init()s issues, however none of them solved the problem (including this almost-exact question). Any idea how to solve it?

Upvotes: 2

Views: 1397

Answers (2)

mclaughj
mclaughj

Reputation: 12865

Overriding the init is now supported. However, you may run into this issue when using a convenience init as the designated initializer if you override the required init. This can be fixed by removing the required init.

For example:

required init() {
    super.init()
}

convenience init(dict: [String: AnyObject]) {
    self.init()

    // custom init work
}

Should become:

convenience init(dict: [String: AnyObject]) {
    self.init() // still calling self.init(), not super.init()

    // custom init work
}

Upvotes: 5

segiddins
segiddins

Reputation: 4120

RealmSwift.Object does not currently support subclasses adding new required initializers, only convenience initializers. More details about initializers and what's not yet supported can be found at https://github.com/realm/realm-cocoa/issues/1849.

Upvotes: 0

Related Questions