Wonder if anyone could shed some light on this messaging construct:
The documentation says that messages appear btwn brackets [] and
that the msg target/object is on the left, whilst the msg itself (and any parameters) is on the right:
[msgTarget msg], e.g., [myArray insertObject:anObject atIndex:0]
OK, simple enough… but then they introduce the idea that it’s convenient to nest msgs in lieu of the use of temporary variables–I’ll take their word for it–so the above example becomes:
[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0]
In other words, [myAppObject theArray]
is a nested msg, one, and, two, ‘theArray’ is the ‘message’. Well, to say I find this confusing is a bit of an understatement … Maybe it’s just me but ‘theArray’ doesn’t evoke a message semantically or grammatically. What this looks like to a guy who knows Java is a type/class. In Java we do things like
Class objectInstance = new Class() ...
the bit to the left of the assignment operator is what this so-called nested message reminds me of … with object and class/type positions switched of course. Anyway, any insight much appreciated.
In Objective-C, by convention, you refer to properties with dot notation. Thus, you write myAppObject.theArray
instead [myAppObject theArray]
.
In Objective-C the default getter is the name of the variable instead getVariable
. For example, writing
@property NSArray *theArray;
creates an instance variable _theArray
and generates the following accessor:
-(NSArray*) theArray { return _theArray; }
So by sending theArray
as a message you are actually invoking a method. But again, use only dot notation for properties.
1