Updating objects is just assign a value to a property within a write transaction. See our documentation.
https://realm.io/docs/swift/latest/#updating-objects
So you don’t need to delete and then add an object. Just assign new value to the property in write transaction like the following.
let workouts = realm.objects(WorkoutsCount.self).filter("date = %@", removeTodaysItem)
let realm = try! Realm()
if let workout = workouts.first {
try! realm.write {
workout.date = today
workout.count = plusOne
}
}
FYI: Please don’t use string interpolation in a query. Generally constructing an important string by string interpolation is bad practice. Use NSPredicate’s string substitute syntax like filter("date = %@", removeTodaysItem).