Reputation: 403
When reading the docs, it says I should do the following to update a single (or multiple) field through a dictionary:
var person = ["personID": "My-Primary-Key", "name": "Tom Anglade"]
// Update `person` if it already exists, add it if not.
let realm = try! Realm()
try! realm.write {
realm.add(person, update: true)
}
I have no idea how they can get to compile, because I get this error:
Cannot convert value of type '[String : String]' to expected argument type 'Object'
I want to dynamically update multiple field throughout a dictionary, is this possible?
Upvotes: 0
Views: 619
Reputation: 54716
I don't think that tutorial is correct, even though it's from the official website written by Realm engineers. If you look at the official documentation of RealmSwift, you can see that both versions of the add
function accept an argument subclassing Object
.
The function you are looking for and that should be mentioned in the tutorial is public func create<T: Object>(_ type: T.Type, value: Any = [:], update: Bool = false) -> T
, which you can use to update an existing object from a Dictionary
.
Your code should look like:
var person = ["personID": "My-Primary-Key", "name": "Tom Anglade"]
// Update `person` if it already exists, add it if not.
let realm = try! Realm()
try! realm.write {
realm.create(Person.self, value: person, update: true)
}
Upvotes: 2