iOS 訊息(即方法調用)的兩個隱藏參數 :self 和 _cmd

來源:互聯網
上載者:User

標籤:android   des   style   blog   class   code   

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—althoughrespondsToSelector: 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.


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.