Methods for calling methods directly and sending messages to objects (i.e., perfromselector and nsinvocation)
Performselector is the runtime system is responsible for the method to find, at compile time do not do any verification, if the direct call to compile is automatically verified. If the ImageDownloader:didFinishWithImage:image: does not exist, then the direct call can be found at compile time (with Xcode can be written to find out), But using performselector words must be at runtime to discover (at this time the program crashes); Cocoa supports adding a method to a class at run time, that is, the method compiles without being present, but at runtime, it is necessary to use Performselector to invoke. So sometimes if performselector is used, the check method is used for the robustness of the program.
-(BOOL) Respondstoselector: (SEL) Aselector;
Transfer from http://blog.csdn.net/meegomeego/article/details/20041887
1, Perfromselector
2, Nsinvocation
Nsinvocation can pass more than two parameters compared to the perfromselector benefits.
1) nsinvocation use method one
//1. Create a "method call" object: Nsinvocation (Invocation's English Meaning: call)//1>, set the "method signature" object, how to understand this method signature??? Nsmethodsignature *signature =[Viewcontroller instancemethodsignatureforselector: @selector (sendMessageWithNumber:andContent:status:)]; Nsinvocation*invocation =[Nsinvocation invocationwithmethodsignature:signature]; //2>, who is to execute the---insideInvocation.target =Self ; //3>, what methodInvocation.selector=@selector (sendMessageWithNumber:andContent:status:); //4>, passed parameters//Note: The custom parameter index starts at 2, and 0 is Self/1 is _cmd;//The method name saved in the method signature must be the same as the method name saved in the invocation selector;NSString *number =@"10086"; NSString*content =@"Hello"; NSString*status =@"sent successfully"; [Invocation setargument:&number Atindex:2]; [Invocation setargument:&content Atindex:3]; [Invocation setargument:&status Atindex:4]; //2. Execute Call[Invocation invoke];
2) Nsinvocation Way two: Add a category to NSObject
. h file
#import <Foundation/Foundation.h>@interface nsobject (CY)-(ID) Performselector: (SEL) aselector withobjects: (Nsarray *) objects; @end
. m file
#import "Nsobject+cy.h"@implementationNSObject (CY)- (ID) Performselector: (SEL) aselector withobjects: (Nsarray *) objects{//The name/parameter/return value type of the method is saved in the signature//Note: Signatures are generally used to set parameters and get return values, and the method's call does not have much of a relationshipNsmethodsignature *signature = [selfclass] Instancemethodsignatureforselector:aselector]; //0. Determine if the incoming method exists and handle it if it does not exist if(Signature = =Nil) { //throws an exception if the method does not exist//@throw [nsexception exceptionwithname:@ "Small Code Gothic class specific exception" reason:@ "You Too SB" Userinfo:nil];NSString *info = [NSString stringWithFormat:@"The%@ method cannot be found", Nsstringfromselector (Aselector)]; [NSException Raise:@"Specific Exception"Format:info, nil];//return nil; } //1. Create a Nsinvocation object//the object/method name/parameter/return value to which the method belongs is saved in NsinvocationNsinvocation *invocation =[Nsinvocation invocationwithmethodsignature:signature]; Invocation.target=Self ; Invocation.selector=Aselector; //Note: If you need to set parameters by traversing to invocation, you cannot traverse the objects array//because the length of the objects array is not controllable//Note: The number of arguments obtained by the Numberofarguments method is the one containing the self and the _cmdNsuinteger Argscount = signature.numberofarguments-2;//3Nsuinteger arrcount = Objects.count;//4 2Nsuinteger count =MIN (Argscount, arrcount);//for (int i = 0; i < Objects.count; i++) { for(inti =0; I < count; i++) { IDobj =Objects[i]; //determine if the parameter you want to set is Nsnull, and if it is set to nil if([obj Iskindofclass:[nsnullclass]]) {obj=Nil; } [invocation setargument:&obj atindex:i +2]; } //2. Invoking the Invoke method of the Nsinvocation object[invocation invoke]; //3. Get the return value of a method IDres =Nil; //3.1 Judging The method currently called, whether there is a return value//NSLog (@ "Methodreturntype =%s", signature.methodreturntype);//NSLog (@ "methodreturnlength =%zd", signature.methodreturnlength); if(Signature.methodreturnlength! =0) {[Invocation getreturnvalue:&Res]; } returnRes;}@end
Two ways to call object methods directly