RealmSwift Initializers - Xcode Patch - It Continues to Mistake

I cannot get Realm to work, when I want to provide an initializer for a class, Xcode offers errors endlessly.

I decided to upload two screenshots instead of a code snippet to make it easier to view errors.

enter image description here

I follow the recommendations and end with this

enter image description here

The last error says "Use undeclared type" RLMObjectSchema "

I am using the latest version of RealmSwift version 0.99

+6
source share
2 answers

The recommended way is to create a convenience initializer in order, as shown below:

class Item: Object { dynamic var isBook: Bool = true dynamic var numberOfPages: Double = 0 dynamic var isInForeignLanguage: Bool = true dynamic var isFictional: Bool = true dynamic var value: Int { get { return calculalatedValue() } } convenience init(isBook: Bool, numberOfPages: Double, isInForeignLanguage: Bool, isFictional: Bool) { self.init() self.isBook = isBook self.numberOfPages = numberOfPages self.isInForeignLanguage = isInForeignLanguage self.isFictional = isFictional } ... } 

You cannot skip the default initializer, because Realm needs a default initializer to instantiate objects for queries. When requested by Realm, Realm internally initializes the default initializer for creating objects.

You can also override the default initializer, but we do not recommend using it. Because when you override the default initializer, you must override the other necessary initializers inherited from the ObjC level due to Swift system limitations. You must also import both Realm frames and RealmSwift . Because there is Objective-C only the class in the parameters of these initializers.

 import RealmSwift import Realm // Need to add import if you override default initializer! class Item: Object { dynamic var isBook: Bool = true dynamic var numberOfPages: Double = 0 dynamic var isInForeignLanguage: Bool = true dynamic var isFictional: Bool = true dynamic var value: Int { get { return calculalatedValue() } } required init() { super.init() } required init(realm: RLMRealm, schema: RLMObjectSchema) { super.init(realm: realm, schema: schema) } required init(value: AnyObject, schema: RLMSchema) { super.init(value: value, schema: schema) } 
+10
source

Try:

 required convenience init(...) { self.init() ... } 

See https://github.com/realm/realm-cocoa/issues/1849

+1
source

All Articles