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?
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.
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.
This statement is telling your view self.view
(which is the parent view "self") to addSubview
label
.