Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
OK -- got it. Thanks very much! I would upvote you, but I'm new around here and apparently haven't yet earned the right. –  user10756 May 15 '13 at 12:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.