There are two ways to invoke the message of an object in OC:
# # #. Performanceselector:withobject:
#2. Nsinvocation.
The first performaceselector is more common and simpler. But this method can only pass a maximum of 2 parameters
When you need more than 2 parameters, you can only use Nsinvocation.
Directly on the code, it will be clearly annotated
- (void) viewdidload {[Super viewdidload]; //The method of calling three parameters with Performanceselector, but passing only 2 parameters, so that the third parameter of the method will automatically take the second value we pass[Self performselector: @selector (PRINTSTR1:STR2:STR3:) Withobject:@"str1"Withobject:@"str2"]; //1. Create a method signatureNsmethodsignature *signature =[Viewcontroller instancemethodsignatureforselector: @selector (PRINTSTR1:STR2:STR3:)]; //2. Create a Nsinvocation object based on the method signature, it is best to first determine whether the previous created signature is nil, the method does not exist is nilNsinvocation *invocation =[Nsinvocation invocationwithmethodsignature:signature]; //to set the caller of a methodInvocation.target =Self ; //Set the method name, which must be consistent with the method name in the method signature classInvocation.selector =@selector (PRINTSTR1:STR2:STR3:); //Customizing three parametersNSString *STR1 =@"First argument"; NSString*STR2 =@"Second Argument"; NSString*STR3 =@"third Argument"; //Add the parameter from the second position, since the previous two positions are already occupied, respectively, when self (target), selector (_cmd)[Invocation setargument:&str1 Atindex:2]; [Invocation setargument:&STR2 Atindex:3]; [Invocation setargument:&STR3 Atindex:4]; //3. Call the Invoke method[invocation invoke]; }- (void) PRINTSTR1: (NSString *) str1 STR2: (NSString *) str2 STR3: (NSString *) STR3 {NSLog (@"%@", STR1); NSLog (@"%@", STR2); NSLog (@"%@", STR3);}@end
The output is:
.- on- . One: -:07.398bezierpathdemo[1203:97184] str1 .- on- . One: -:07.398bezierpathdemo[1203:97184] str2 .- on- . One: -:07.399bezierpathdemo[1203:97184] str2 .- on- . One: -:07.399bezierpathdemo[1203:97184] First argument .- on- . One: -:07.399bezierpathdemo[1203:97184] Second argument .- on- . One: -:07.399bezierpathdemo[1203:97184] Third argument
Nsinvocation when using the following three places to pay attention to
1, if the calling method does not exist
//此时我们应该判断方法是否存在,如果不存在这抛出异常if (signature == nil) {//aSelector为传进来的方法NSString *info = [NSString stringWithFormat:@"%@方法找不到", NSStringFromSelector(aSelector)];[NSException raise:@"方法调用出现异常" format:info, nil]; }
2. The number of parameters of the method does not match the number of parameters array elements in the outside world
//it is not possible to set parameters by traversing the parameter array, because the number of parameters passed in is not controllable // Therefore, the number of parameters obtained by the Numberofarguments method is the one that contains the self and the _cmd, then compares the parameters required by the method and the number of parameters passed in by the outside world, and takes the minimum value between them 2; Nsuinteger arrcount = Objects.count; nsuinteger count = MIN (Argscount, Arrcount); for (int i = 0; i < count; i++) { Span class= "Hljs-keyword" >id obj = objects[i]; //determine if the parameter to be set is nsnull, if it is set to nil if ([obj iskindofclass:[< Span class= "hljs-built_in" >nsnull class]] {obj = NIL;} [Invocation setargument:&obj atindex:i + 2];}
3. Determine if the currently called method has a return value
//方法一:id res = nil;if (signature.methodReturnLength != 0) {//有返回值 //将返回值赋值给res [invocation getReturnValue:&res];}return res;//方法二://可以通过signature.methodReturnType获得返回的类型编码,因此可以推断返回值的具体类型
Use of nsinvocation in Objective-c