Search code examples
swiftrealmrealm-mobile-platformrealm-ios

'RLMException', reason: 'Table has no columns'


I have checked the related article with this problem. Issue in adding data in Realm in iOS

It seems only issue the previous post that folks were not adding dynamic to variables. that is not my case. I am not finding any other reason at all why it would fail.

Build target 12.2, swift version: Swift 5, realm version: swift-10.2.1

my class:

class Person: Object {
    dynamic var id = UUID().uuidString
    dynamic var name = "" 
}

This is how I am trying to add to the realm

    let realm = try! Realm()
    let person = Person()
    person.name = "John Doe"
    try! realm.write {
        realm.add(person)
    }

Any pointers or suggestion would be appreciated. Thanks a lot for reading the post.


Solution

  • You still need the @objc attribute in your variables. Also see here.

    class Person: Object {
        @objc dynamic var id = UUID().uuidString
        @objc dynamic var name = "" 
    }
    

    EDIT:

    For completeness, Realm Object classes can be marked as @objcMembers (Swift 4+) which will make all class properties marked with dynamic to be managed by Realm. See Property Attributes in the Swift guide.