Two hidden parameters for IOS messages (that is, method calls): Self and _cmd

Source: Internet
Author: User

Two hidden parameters for an IOS message (that is, a method call)

The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.


The following excerpt from theobjective-c Runtime programmingGuide


Using hidden parameters
Using Hidden Arguments

When objc_msgSend finds the procedure so implements a method, it calls the procedure and passes it all the arguments in the MES Sage. It also passes the procedure and the hidden arguments:

    • the receiving object

    • the selector for the method

These arguments give every method implementation explicit information about the both halves of the message expression that Invoked it. They ' re said to being ' 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.

Although these parameters are not explicitly declared, the source can still refer to them (just as it can refer to the instance variables of the receiving object). Each method calls the message receiving object Self, and its own selector is called_cmd。 In the following example,_cmdReferenceStrangeMethod of the selector, and SelfReference receiveStrangeThe object of the message.
Although these arguments aren ' t explicitly declared, source code can still refer to them (just as it can refer to the Rece Iving object ' s instance variables). A method refers to the receiving object asself, and to its own selector as_cmd. In the example below,_cmdRefers to the selector for thestrangeMethod andselfTo the object that receives astrangeMessage.

-Strange
{
    ID  target = getthereceiver ();
    SEL method = Getthemethod ();
    if (target = = Self | | method = = _cmd)
        return nil;
    return [target Performselector:method];
}

selfIs the more useful of the arguments. It's, in fact, the receiving object's instance variables is made available to the method definition.



Doesnotrecognizeselector:

Handles messages that are not recognized by the receiver.
Handles messages The receiver doesn ' t recognize.

-(void) Doesnotrecognizeselector: (SEL)aselector

Parameter Parameters

Aselector

A selector is used to identify methods that are not implemented by the receiver and are not recognized by the recipient.
A selector that identifies a method is not implemented or recognized by the receiver.

Discussion Discussion

Whenever an object
The runtime system invokes this method whenever a object receives an aselector message it can ' t respond to or forward. This method, in turn, raises ansinvalidargumentexception, and generates an error message.

Any doesnotrecognizeselector: messages is generally sent only by the runtime system. However, they can be used on program code to prevent a method from being inherited. For example, ansobject subclass might renounce the copy orinit method by re-implementing it to Include adoesnotrecognizeselector: message as follows:

- (ID) Copy


{


    [self doesnotrecognizeselect Or:_cmd];


}


td>

The _cmd variable is a hidden argument passed to every method, that's 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 forwardingC opy messages-althoughrespondstoselector: would still report, the receiver had access to acopy me Thod.

If you override this method, you must call Super or raise ansinvalidargumentexception exception at the End of your implementation. In the other words, the This method must not return normally; It must always result in an exception being thrown.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.