Search code examples
swiftdatabaserealmswift4local-database

Realm: Cannot invoke 'add(_:update:)' with an argument list of type '(String)'


I have the following code

import RealmSwift
class myclass: NSObject {
  let realm = try! realm()
  @objc dynamic var id = String()

  func writeRealmLocal() {
    try! realm.write {
       realm.add(id)
    }
  }
}

But, i throw the following error: Cannot invoke 'add(_:update:)' with an argument list of type '(String)'

I am following the realm documentation but I cannot solve this error, any ideas?

Error Realm and Swift 4


Solution

  • You are using Realm incorrectly. Realm saves object instances, each of which are of type Object. Here is reference for the class.

    If you wish to save you MyClass, you could do the following,

    import RealmSwift
    
    class MyClass: Object {
        @objc dynamic var id = String()
    }
    

    And, use Realm object to store the object.

    let realm = try! Realm()
    realm.write {
     realm.add(MyClass())
    }
    

    I suggest you to go through the Getting Started section in realm.io page. And if you wish to see documentation for classes, go here