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:
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,_cmd
Refers to the selector for thestrange
Method andself
To the object that receives astrange
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 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.