Search code examples
iosobjective-cios6toggle

How does sender.selected = ! sender.selected toggle between selected states?


I'm very new to Objective-C so sorry if this is extremely obvious to many of you, but I'm trying to work out how the following piece of code is actually working:

- (IBAction)chooseColour:(UIButton *)sender {
 sender.selected = !sender.isSelected;
}

Now it obviously toggles between the selected and unselected states of the button sending the action, but what is the code 'sender.selected = !sender.isSelected' actually saying? Is it just 'set the sender selected property to the opposite (i.e. ! not) of the getter'? So if the getter is 'getting' the current selected value as true then it sets the selected property as !true i.e false. Or is this a piece of convenience code that I'm not yet privy to? Because it also seems that '!sender.isSelected' simply means not selected as in

if (!sender.isSelected){
statement
}

i.e. do statement if the sender is not selected. This is no doubt really obvious, just I'm a bit confused with it at the moment.

Thanks!


Solution

  • You are entirely correct, it's calling the getter to obtain the value and calling the setter with the NOT (!) of the value. It isn't Objective-C, it's plain C syntax.