I have an array of custom objects. For simplicity, lets say each object has 3 properties : id
, timestamp
and text
. I am now obtaining a JSON response from my server which contains updated text
for my objects in the array. I need to do the following :
- Obtain the object with the given
id
from my array. - Set the
text
of the object to the updatedtext
WITHOUT changing any of its other properties. - Override the old object with the new and updated one.
I have found the following extension which allows me to find the object in my array.
extension CollectionType {
func find(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
return try indexOf(predicate).map({self[$0]})
}
}
I am then using the following logic to obtain the item from the array.
let my_object = questionImageObjects.find({$0.id== myId})
Now I set the text using my_object.text = currText
.
The last step is to override the old object in the array with the updated one. This is where I am stuck.
Aucun commentaire:
Enregistrer un commentaire