NSInvocation使用demo

// 随便在一个类中添加下面两个方法
- (int)myLog:(int)a param:(int)b parm:(int)c
{
    return a+b+c;
}
- (void)doNSMethodSignatureTest
{
    int a = 1;//参数3(参数1是self,参数2是selector)
    int b = 2;//参数4
    int c = 3;//参数5
    int d = 0;//返回值
    SEL sel1 = @selector(myLog:param:parm:);
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:sel1];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    [invocation setTarget:self];
    [invocation setSelector:sel1];
    [invocation setArgument:&a atIndex:2];
    [invocation setArgument:&b atIndex:3];
    [invocation setArgument:&c atIndex:4];
    [invocation invoke];
    [invocation getReturnValue:&d];
    
    NSLog(@"d:%d", d);
}
// 继续啰嗦一下,下面这个函数该怎么来调用呢?
- (NSString *)testParamForcharValue:(char)charValue//c
                  unsignedCharValue:(unsigned char)unsignedCharValue//C
                         shortValue:(short)shortValue//s
                 unsignedShortValue:(unsigned short)unsignedShortValue//S
                           intValue:(int)intValue//i
                   unsignedIntValue:(unsigned int)unsignedIntValue//I
                          longValue:(long)longValue//l
                  unsignedLongValue:(unsigned long)unsignedLongValue//L
                      longLongValue:(long long)longLongValue//q
              unsignedlongLongValue:(unsigned long long)unsignedlongLongValue//Q
                         floatValue:(float)floatValue//f
                        doubleValue:(double)doubleValue//d
                          boolValue:(BOOL)boolValue//B
                      selectorValue:(int(^)(int, int))selectorValue//:
                          rectValue:(CGRect)rectValue//{name=type...}
                          sizeValue:(CGSize)sizeValue//{name=type...}
                        charPointer:(char *)charPointer//*
                              point:(int *)pint//^
                         classValue:(Class)classValue//#
                      nsstringValue:(NSString *)nsstringValue//@
                       nsarrayValue:(NSArray *)nsarrayValue//@
                            idValue:(id)idValue//@;
                        rectPointer:(CGRect *)rectPointer
{
    NSLog(@"2 + 3 = %d", selectorValue(2, 3));
    pint += 1;
    *rectPointer = CGRectMake(100, 1000, 10000, 100000);
    return @"return string";
}
- (void)callTestParam
{
    SEL sel =  @selector(testParamForcharValue:unsignedCharValue:shortValue:unsignedShortValue:intValue:unsignedIntValue:longValue:unsignedLongValue:longLongValue:unsignedlongLongValue:floatValue:doubleValue:boolValue:selectorValue:rectValue:sizeValue:charPointer:point:classValue:nsstringValue:nsarrayValue:idValue:rectPointer:);
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:sel];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    [invocation setTarget:self];
    [invocation setSelector:sel];
    //参数0是self
    //参数1是selector
    char charParam = 't';
    unsigned char unsignedCharParam = 't';
    short shortParam = 10;
    unsigned short unsignedShortParam = 10;
    int intParam = 10;
    unsigned int unsignedIntParam = 10;
    long longParam = 88;
    unsigned long unsignedLongParam = 88;
    long long longLongParam = 88;
    unsigned long long unsignedLongLongParam = 88;
    float floatParam = 1212;
    double doubleParam = 1313;
    BOOL boolParam = YES;
    int(^blockParam)(int, int) = ^int(int a, int b) { return a + b;};
    CGRect rectParam = CGRectMake(10, 20, 30, 40);
    CGSize sizeParam = CGSizeMake(100, 200);
    char *charPointer = "pcharStr";
    int intValue = 100;
    int *intPointer = &intValue;
    Class classParam = [WJSPatchSrcAnalysisVC class];
    NSString *nsstringParam = @"nsstring value";
    NSArray *nsarrayParam = @[@3, @"str4"];
    NSDictionary *nsdictionary = @{@"strkey":@[@2, @3]};
    CGRect rectValue = CGRectMake(10, 10, 10, 10);
    CGRect *pRectValue = &rectValue;
    NSString *returnValue = @"return string value";
    
    [invocation setArgument:&charParam atIndex:2];//char
    [invocation setArgument:&unsignedCharParam atIndex:3];//unsigned char
    [invocation setArgument:&shortParam atIndex:4];//short
    [invocation setArgument:&unsignedShortParam atIndex:5];//unsigned short
    [invocation setArgument:&intParam atIndex:6];//int
    [invocation setArgument:&unsignedIntParam atIndex:7];
    [invocation setArgument:&longParam atIndex:8];//long
    [invocation setArgument:&unsignedLongParam atIndex:9];
    [invocation setArgument:&longLongParam atIndex:10];//long long
    [invocation setArgument:&unsignedLongLongParam atIndex:11];
    [invocation setArgument:&floatParam atIndex:12];//float
    [invocation setArgument:&doubleParam atIndex:13];//double
    [invocation setArgument:&boolParam atIndex:14];//BOOL
    [invocation setArgument:&blockParam atIndex:15];//block
    [invocation setArgument:&rectParam atIndex:16];
    [invocation setArgument:&sizeParam atIndex:17];
    [invocation setArgument:&charPointer atIndex:18];
    [invocation setArgument:&intPointer atIndex:19];
    [invocation setArgument:&classParam atIndex:20];
    [invocation setArgument:&nsstringParam atIndex:21];
    [invocation setArgument:&nsarrayParam atIndex:22];
    [invocation setArgument:&nsdictionary atIndex:23];
    [invocation setArgument:&pRectValue atIndex:24];
    [invocation invoke];
    [invocation getReturnValue:&returnValue];
    
    NSLog(@"%@", returnValue);
}

你可能感兴趣的:(NSInvocation使用demo)