Random Reminder of the Day: UISwitch isOn vs. on – Object.property Is Not a Variable, It Is a Method.
I know that I should stop talking about objective-C. sorry.
It is interesting to see how coming into the Objective-C language in the last couple of years blinds you to some of the more traditional uses of the language. In particular the dot-syntax hides the underlying message passing model of objective-C. It is of course fully equivalent, but it is also compiler checked, which makes it easy to forget that @properties are merely getter/setter methods. Of course, we all learn that they are equivalent, but without the practice of coding without dot-syntax, it is easy to forget the implications.
I have been thinking about this recently, because I was contributing to an open-source project on github, and the coding in it was done almost entirely in braces-only syntax. It was an excellent exercise to stick with that entirely in my contributions. It was revealing to be constantly reminded that properties are not variables but methods.
To be explicit about this:
object.ivar = value
is not the same as
_ivar = value
but rather is equivalent to
The (trivial) spark of this post, was a target-action method from a UISwitch that I wrote, passing the UISwitch object as
(id)Sender, and elsewhere, I had referred to the on/off property of my typed UISwitch object with
switch.on = .... However, when I tried to type
[sender on] for logging purposes, it was an error. I then typecast, logged, switch.on and it worked of course. But I was curious, and checked the documentation. Sure enough, it has a defined “isOn” getter method for the “on” property.
which means that
variable = switch.on;
is equivalent to
variable = [switch isOn];
variable = [switch on];
Anyway, this couldn’t be more basic, but it was a good reminder for me.