Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Code Understanding

Tags:

objective-c

I have the following code:

- (IBAction)buttonSectionPressed:(id)sender {

    if ([self.switchReloadOnlyDontToggleVissibility isOn]) {
        [self updateCells:self.section2Cells];
    } else {
        BOOL hide = ([sender tag] == 0);
        [self cells:self.section2Cells setHidden:hide];
    }

    [self reloadDataAnimated:([self.switchAnimated isOn])];
}

I have a question with

BOOL hide = ([sender tag] == 0);

Is it checking to see if (sender.tag == 0) then assign it to hide? So, (if sender.tag != 0), hide does not exist?

like image 577
user1107173 Avatar asked Feb 16 '23 16:02

user1107173


1 Answers

This expression works as follows:

  • Evaluates [sender tag]
  • Compares the result to zero
  • If the result is zero, hide is set to YES; otherwise, it is set to NO.

This could also be done with the equivalent expression that uses property syntax:

BOOL hide = (sender.tag == 0);

Finally, you can drop the hide variable altogether:

[self cells:self.section2Cells setHidden:(sender.tag == 0)];
like image 57
Sergey Kalinichenko Avatar answered Mar 31 '23 10:03

Sergey Kalinichenko