Objective-C object messaging

1.

To summarize, the following are the key elements of Objective-C object messaging:

  • Message: A name (the selector) and a set of parameters sent to an object/class.
  • Method: An Objective-C class or instance method that has a specific declaration comprised of a name, input parameters, a return value, and the method signature (the data type(s) for the input parameters and return value).
  • Method binding: The process of taking a message sent to a particular receiver and finding and executing the appropriate method. The Objective-C runtime performs dynamic binding of messages to method calls.

2.

Selectors

In Objective-C object messaging, a selector is a text string that refers to a method and can be sent to an object or a class. The Objective-C runtime uses selectors to retrieve the correct method implementation for a target object/class. A selector is represented as a text string broken up into segments, with a colon placed at the end of each segment that is followed by a parameter:

nameSegment1:nameSegment2:nameSegment3:


3.

The SEL Type

Up to this point, you have defined a selector as a text string that is part of a message in a message-passing expression; now, you’ll examine the selector type. A selector type (SEL) is a special Objective-C type that represents a unique identifier that replaces a selector value when the source code is compiled. All methods with the same selector value have the same SEL identifier. The Objective-C runtime system ensures that each selector identifier is unique. A variable of type SEL can be created using the @selector keyword.

SEL myMethod = @selector(myMethod);

So why would you create SEL variables? Well, the Objective-C runtime system (via NSObject) includes many methods that utilize variables of type SEL as parameters to dynamic methods. In addition to obtaining information about objects and classes, NSObject includes several methods for invoking a method on an object using a selector parameter. The following example uses the NSObjectinstance method performSelector:withObject:withObject: to invoke a method specified by the selector variable.

[myCalculator performSelector:@selector(sumAddend1::) withObject:[NSNumber numberWithInteger:25]
                         withObject:[NSNumber numberWithInteger:10]];

The @selector directive creates a selector variable at compile time. You can also create a selector at runtime with the Foundation Framework NSSelectorFromString function. In this case, the previous example is updated as follows:

 SEL selector = NSSelectorFromString(@"sumAddend1::");
 [myCalculator performSelector:selector withObject:[NSNumber numberWithInteger:25]
                         withObject:[NSNumber numberWithInteger:10]];


4.
NSStringFromSelector(_cmd)

The input parameter for this function is a variable of type SEL. So what’s this _cmd parameter and where did it come from? Well, _cmd is an implicit parameter (available in the implementation of every Objective-C method but not declared in its interface) that holds the selector of the message being sent. Thus, the expression NSStringFromSelector(_cmd) returns a text string for the selector of the method being invoked.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值