2

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.

1 Answer 1

2

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
  • OK -- got it. Thanks very much! I would upvote you, but I'm new around here and apparently haven't yet earned the right. Commented May 15, 2013 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.