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:
So the message
viewis being sent toself, the reciever. The result of that is then in the receiver position for the other message, which isaddSubview:. You’re right aboutlabelbeing the argument toaddSubview:.In English, then, this is: “Add
labelas a subview ofself‘sview” or “sendaddSubview:, passinglabel, to the result of sendingviewtoself“.*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.