I want to make any row from 'false' to 'true' or vice versa depending on table view did select and did deselect method. That mean's I need to update the existing value. How to do that. My code is not working it's creating another row instead of updating the existing one.
Here is the table
Here is the model
import UIKit
import RealmSwift
class TodoData: Object {
@objc dynamic var todos: String = String()
@objc dynamic var times: String = String()
@objc dynamic var rows: Bool = Bool()
}
Here is the code I wrote for updating the value of the existing row:
let data = TodoData()
do {
try realm.write {
data.rows = true
realm.add(data)
}
} catch let error as NSError {
print(error.localizedDescription)
}
If you literaly want to:
I want to make any row from 'false' to 'true' or vice versa
Here's the code that will make all false rows true, and all true row false.
let toDoResults = realm.objects(TodoData.self)
for toDo in toDoResults {
try! realm.write {
toDo.rows = !toDo.rows
}
}
I think though, you just want to toggle a row when the user makes a change in your tableView.
let selectedTodo = "a"
let results = realm.objects(TodoData.self).filter("todos == %@", selectedTodo)
if let thisTodo = results.first {
try! realm.write {
thisTodo.rows = !thisTodo.rows //will toggle from t to f and from f to t
}
}