Message Dispatch

The receiving object (i.e., receiver) of a message determines at runtime which of its instance methods to invoke.

The Objective-C syntax for sending a message to (i.e., invoking a method on)  an object is

[receiver messageNameParams]

The messageNameParams information identifies the  method’s actual name and parameter values (if any);

and brackets enclose the message.

The syntax  for the messageNameParams is keyword1:value1 keyword2:value2 ... keywordN:valueN

A colon separates each method signature keyword and its associated parameter value.

扫描二维码关注公众号,回复: 350233 查看本文章

It declares that a parameter is required.

If a method has no parameters, the colon is omitted 

(and that method has only one keyword).

Take the following message, for example:

[orderObject addItem:burgerObject forPrice:3.50];

The message name is addItem:forPrice:;

The message parameter values are burgerObject (for keyword addItem) and 3.50 (for keyword atPrice).

Objective-C supports type polymorphism , whereby different receivers can have different implementations of the same method.

The type of receiver is determined at runtime; consequently, different receivers can do different things in response to the same message.

In sum, the result of a message can’t be calculated from the message or method name alone;it also depends on the object that receives the message.

Objective-C provides language-level support for an alternative syntax—dot notation, which simplifies invoking property accessor methods.

The dot syntax for retrieving a property value is objectName.propertyName

The dot syntax for setting a property value is objectName.propertyName = Value

Dot syntax is merely an alternative syntax and not a mechanism for directly accessing a property-backed instance variable. The compiler transforms these statements into the corresponding property accessor methods.

Class methods are commonly used to create new instances of a class (i.e., as factory methods), or for accessing shared information associated with a class.

Objective-C augments its support for object messaging with language-level features that enable you

to determine at runtime the method to be invoked, and even change the method implementation.

猜你喜欢

转载自zsjg13.iteye.com/blog/2260019
今日推荐