Wilson
Wilson

Reputation: 49

Setting a bool variable to itself

class ChecklistViewController: UITableViewController {
    var row0text = "Walk the dog"
    var row1text = "Brush teeth"
    var row2text = "Learn iOS development"
    var row3text = "Soccer practice"
    var row4text = "Eat ice cream"

    var row0checked = false
    var row1checked = false
    var row2checked = false
    var row3checked = false
    var row4checked = false

    override func tableView(tableView: UITableView,                       didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            if indexPath.row == 0 {
                row0checked = !row0checked
                if row0checked {
                    cell.accessoryType = .Checkmark
                } else {
                    cell.accessoryType = .None
                }
            } else if indexPath.row == 1 {
                row1checked = !row1checked
...

What is the purpose of row0checked = !row0checked here?

I know that ! means not here, but I could not understand this part of code.

Upvotes: 1

Views: 63

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

What row0checked = !row0checked does is that it takes the opposite value, for example:

If the first row is checked and you press it it will be unchecked.

So if row0checked is true and you run row0checked = !row0checked row0checked will be false and vice versa.

This is done to check/uncheck a checklist item and show it by setting the cell.accessoryType.

Upvotes: 3

Related Questions