iOS 消息(即方法调用)的两个隐藏参数 :self 和 _cmd

iOS 消息(即方法调用)的两个隐藏参数

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


以下摘自《Objective-C Runtime Programming Guide


使用隐藏参数 
Using Hidden Arguments

When objc_msgSend finds the procedure that implements a method, it calls the procedure and passes it all the arguments in the message. It also passes the procedure two hidden arguments:

  • The receiving object

  • The selector for the method

These arguments give every method implementation explicit information about the two halves of the message expression that invoked it. They’re said to be “hidden” because they aren’t declared in the source code that defines the method. They’re inserted into the implementation when the code is compiled.

尽管这些参数不是显式声明的,源码仍能引用它们(正像它能引用接收对象的实例变量一样)。每个方法都把消息接收对象称作 self,而自身的选择器称作_cmd。下面的示例中,_cmd 引用strange 方法的选择器,而self 引用接收 strange 消息的对象。
Although these arguments aren’t explicitly declared, source code can still refer to them (just as it can refer to the receiving object’s instance variables). A method refers to the receiving object as self, and to its own selector as _cmd. In the example below, _cmd refers to the selector for the strange method and self to the object that receives a strange message.

- strange
{
    id  target = getTheReceiver();
    SEL method = getTheMethod();
 
    if ( target == self || method == _cmd )
        return nil;
    return [target performSelector:method];
}

self is the more useful of the two arguments. It is, in fact, the way the receiving object’s instance variables are made available to the method definition.



doesNotRecognizeSelector:

处理接收者无法识别的消息。
Handles messages the receiver doesn’t recognize.

- (void)doesNotRecognizeSelector:(SEL)aSelector

参数 Parameters

aSelector

一个 selector 用于标识未被接收者实现和不能被接收者识别的方法。
A selector that identifies a method not implemented or recognized by the receiver.

讨论 Discussion

无论何时一个对象
The runtime system invokes this method whenever an object receives an aSelector message it can’t respond to or forward. This method, in turn, raises anNSInvalidArgumentException, and generates an error message.

Any doesNotRecognizeSelector: messages are generally sent only by the runtime system. However, they can be used in program code to prevent a method from being inherited. For example, anNSObject subclass might renounce the copy orinit method by re-implementing it to include adoesNotRecognizeSelector: message as follows:

- (id)copy


{


    [self doesNotRecognizeSelector:_cmd];


}


The _cmd variable is a hidden argument passed to every method that is the current selector; in this example, it identifies the selector for thecopy method. This code prevents instances of the subclass from responding tocopy messages or superclasses from forwardingcopy messages—although respondsToSelector: will still report that the receiver has access to acopy method.

If you override this method, you must call super or raise anNSInvalidArgumentException exception at the end of your implementation. In other words, this method must not return normally; it must always result in an exception being thrown.


你可能感兴趣的:(<=即时总结=>,平台-iOS,语言-C_C++_OC)