What is the correct way to read this Objective-C statement?

Go To StackoverFlow.com

1

I'm starting to learn how to read message syntax in Objective-C and want to reinforce good understanding of message syntax (I come from a Java/C#/Ruby background).

I'm currently looking at the statement:

[self.view addSubview:label];

"Send this message to the addSubView method, with label as the argument, the mthod is on the view object on self. "

("on" isn't really a great way to describe the "dot notation" for the objects. I'm open to a better way to transcribe it!)

How do you read and interpret the above statement?

2012-04-03 23:58
by Jimmy Lyke


4

This is actually a nested message send, althogh the dot syntax makes that confusing.* The distinction between messages and methods in ObjC can also be a bit confusing at first. Generally, one can talk about them as equivalent, but strictly, a message is sent to an object; the message is looked up in the object's method list, and then the associated method is called.**

It can be rewritten:

[[self view] addSubview:label];

So the message view is being sent to self, the reciever. The result of that is then in the receiver position for the other message, which is addSubview:. You're right about label being the argument to addSubview:.

In English, then, this is: "Add label as a subview of self's view" or "send addSubview:, passing label, to the result of sending view to self".


*The dot syntax is intended as sugar for property access, that is, for using the property's setter and getter methods; by default, the name of the getter is the same as the name of the property itself.

**Should the method not be found, the object can do other things with the message. The only real difference that this method/message distinction makes is that the method associated with a message can be changed at runtime -- dynamic binding.

2012-04-04 00:04
by Josh Caswell
If you want understand Objective-C or any language you should - after the first introduction - look at the implementation. It's pretty important to understand how this is done, because the next confusion with message/method is the selector which is in fact a c string used as a key in the hashtable to find the method that reacts to the message - Lothar 2012-04-04 00:11
(Re: original answer) Thanks, this was exactly the in-depth type of explanation I was looking to augment my understanding. This scenario of message sending sounds a lot like vtables from C++; are they used to accomplish similar functionality - Jimmy Lyke 2012-04-04 00:20
@Jimmy: Sorry, I'm not sure. I know almost nothing about C++. Many of the details of ObjC dispatch are explained at bbum's blog: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map - Josh Caswell 2012-04-04 00:25
Thanks for the link and I appreciate your help - Jimmy Lyke 2012-04-04 01:04


3

It calls the method addSubView: on the UIView that is a property of self (presumably a UIViewController instance or subclass of one, with the var label as an argument, presumably that is a UILabel instance.

2012-04-04 00:03
by No Grabbing


0

This statement is telling your view self.view (which is the parent view "self") to addSubview label.

2012-04-04 00:03
by WrightsCS