views:

108

answers:

1

Hi, I'm a beginner following a book for creating iPhone apps.

One of the steps was writing "UISwitch *whichSwitch = whichSwitch.isOn;" and I was just curious as to where "isOn" came from?

In the documentation:

on
A Boolean value that determines the off/on state of the switch.

@property(nonatomic, getter=isOn) BOOL on

What does that "getter=isOn" part mean? My ultimate reason for asking this question is because I want to know what I should do when I come across a similar situation for different UI elements.

Oh yeah, is this like the thing where properties create a "setSomething" mutator and "something" accessor? Except that for booleans it is "isOn" and "on"?

Thanks.

+6  A: 

Properties are basically shorthand for generating methods later (the actual creation is done by @synthesize directives in the implementation file). The getter=isOn inside the @property does indeed mean that the getter method has the name isOn.

Properties by default will create a getter with the same name as the ivar and a setter with set prepended. Changing the getter name (or its setter, with the setter= syntax) is all this property directive does. You should do this only for boolean or similar variables - other variables should have a getter with the same name as the variable.

Tim
thank you! perfect answer
Devoted
and sorry, I just realized that this was probably an easy enough question that I could have just looked up...its just that I'm new and the documentation still scares me a bit
Devoted
Don't be afraid of the docs - they can't hurt you :) if you're just starting out, the Objective-C Programming Language document is very helpful, especially (for this question) the part about Declared Properties: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html
Tim
The documentation is long-winded, but otherwise very accessible. Don't be afraid!http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.htmlThe API documentation, on the other hand, leaves something to be desired.
Daniel Yankowsky