Yeah, there are a lot of details that I could/should have put in, like using 'self.' to indicate that these are properties, not variables declared in the interface, but I'd hoped this should be mostly correct enough to get the basic concepts across and why they are important.
One issue that certainly can be confusing to new folks with the accessor methods is that you won't see them in your code at all, even when the compiler makes the methods. This is important as your (better) explanation mentions that the accessor methods will be called "if they exist". So, when properties are synthesized, manually, or by the compiler transparently, they will be created, but you'll never see them in your code (unless you manually enter the methods yourself). That, in itself, is another lovely little detail that often escapes new adopters of Objective-C.
Also, it wasn't so long ago (pre Xcode 4.4.1, I think?) that the @synthesize wasn't done automatically (and also invisibly to the user) in the processing of the .m file so, now for new folks who start as of Xcode 5.0.2, what's really happening behind the scenes (and that invisible accessor methods are created at all and/or are used at all) certainly can be lost on the new user.
Nice little explanation of the underscore prefix, BTW. On Dec 19, 2013, at 6:53 PM, Chris Page wrote: On Dec 19, 2013, at 2:53 PM, Alex Zavatone < email@hidden> wrote: Accessing the property directly:
Getting NSBunnies *myBunnies = myAwesomeObject.bunnies; // syntax is: object dot property.
Setting myAwesomeObject.bunnies = [[NSBunnies alloc]initWithMoreBunnies: 5];
Note: These are not necessarily direct access. This syntax will call the getter and setter methods if they exist; otherwise, it will perform direct access. The syntax for direct access is to refer to the ivar on its own (I’ve renamed “bunnies” to “_bunnies” to follow convention):
NSBunnies *myBunnies = _bunnies; // syntax is: object dot property. _bunnies = [[NSBunnies alloc]initWithMoreBunnies: 5];
The key here is that there is no object preceding “_bunnies”—no Objective-C message will be sent—so the compiler looks for a local variable or ivar, then parameters, then globals; ivars are just another variable that’s in scope. Of course, this only works inside methods of a class that has this ivar, and this is intentional: normally, the public interface for a class only exposes accessor methods (or @property declarations) and direct ivar access is a private implementation detail that only that class’s methods should do. By default, @property names the ivar and the getter with the same name and you always access it through the getter, even inside that class’s methods; generally, it should be a special case to use direct ivar access, like when you’re implementing accessors. If you want to use direct ivar access, the convention is to name the ivar with an underscore prefix to make it clear that it’s using direct access, and so that it has a different name from the getter accessor.
-- Chris Page
The other, other AppleScript Chris
|