Reputation: 10609
I have a simple object:
class MyObject : Object {
dynamic var dummyField: String!;
}
and another object which inherits from MyObject
:
class MyOtherObject : MyObject {
dynamic var anotherDummyField: String!;
}
Now I do the following. I have a Realm List of MyObject
and I create an instance of MyOtherObject
and try to save it inside the list:
class Operator {
internal var myObjects: List<MyObject>!;
internal var myObject: MyObject!;
func operate() {
self.myObject = MyOtherObject();
self.myObject.dummyField = "dummy field";
self.myObject.anotherDummyField = "another dummy field";
self.myObjects = List<MyObject>();
self.myObjects.append(myObject); // crash!
}
}
It crashes with the error:
Terminating app due to uncaught exception 'RLMException', reason: 'Object type 'MyOtherObject' does not match RLMArray type 'MyObject'.'
Since MyOtherObject
is a subclass of MyObject
I cannot understand why the app is crashing at this point with this error message.
Upvotes: 2
Views: 1052
Reputation: 170
self.myObjects = List<MyObject>();
This is not a proper value
self.myObjects = List<Needs actual object here not class>();
Upvotes: 1
Reputation: 8138
Realm List
s are not covarient. You can only store objects of exactly the declared type in them.
Upvotes: 6