Find an item and change value in custom object array – Swift

For me, the above answer did not work. So, what I did was first find the index of the object that I want to replace then using the index replace it with the new value

if let row = self.upcoming.index(where: {$0.eventID == id}) {
       array[row] = newValue
}

In Swift 5.0:

if let row = self.upcoming.firstIndex(where: {$0.eventID == id}) {
       array[row] = newValue
}

Leave a Comment