Search code examples
iosswiftrealm

setValue(value, forKey) somehow changes the whole array in Realm


I try to change the value in the object I created. But somehow after this easy operation the whole array is editing.

This is Swift 4.2, I'm using Realm and iOS 12.4.

let index = indexes[i].map { String($0) }.joined(separator: "")
//index is "00"

let obj = object[index] 
print(object)

let key = (o.value)["values"].array![Int(key.0)!]
obj?.setValue(res, forKey: key["prop_name"].string!) //example data provided in screenshot

print("Adding property " + (key["prop_name"].string! ) + " with value '" + (key["value"].string!) + "' = " + res) //in example I add property 'number' with value = 1
print(object)

I expect one Object to change.

Swift's prints


Solution

  • Poor me.

    Realm links Object you created before with an array.

    So I needed to create different objects and put them in this array. I edited the same Object every time.