Search code examples
iosswiftrealm

"Invalid property name" error when filtering objects based on custom object property


I want to filter out objects based on a custom object property called "type" using the following statement:

realm.objects(MyRealmObject.self).filter("type != %@", MyCustomObject.self)

I get the following error then:

caught "Invalid property name", "Property 'type' not found in object of type 'MyRealmObject'".

The definition of MyRealmObject is following:

final class MyRealmObject: Object {
    @objc dynamic var id: String = ""
    @objc dynamic var title: String = ""
    var type: MyCustomObject? = nil
}

What syntax in Realm's "filter" statement should I use to filter based on "type" property of MyRealmObject?


Solution

  • I think you need to make its declaration like that :

     @objc dynamic var type: MyCustomObject? = nil
    

    adding @objc dynamic like other properties.