zoukankan      html  css  js  c++  java
  • NSMethodSignature和NSInvocation的使用(方法传多参数)

    动态调用方法时会用到,例子 

    -(NSString *)myMethod:(NSString *)param1 withParam2:(NSNumber *)param2 

        NSString *result = @"objc"; 
        NSLog(@"par = %@",param1); 
        NSLog(@"par 2 = %@",param2); 
        return result; 

    -(void)invokeMyMethodDynamically 

        SEL selector = @selector(myMethod:withParam2:); 
        NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selector];//获得类和方法的签名 
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; 
        //从签名获得调用对象 
        [invocation setTarget:self]; 
        //设置target 
        [invocation setSelector:selector];//设置selector 
        NSString *returnValue = nil; 
        NSString *argument1 = @"fist"; 
        NSNumber *argument2 = [NSNumber numberWithInt:102]; 
        [invocation setArgument:&argument1 atIndex:2];//设置参数,第一个参数index为2 
        [invocation setArgument:&argument2 atIndex:3]; 
        [invocation retainArguments];//retain一遍参数 
        [invocation invoke];//调用 
        [invocation getReturnValue:&returnValue];//得到返回值,此时不会再调用,只是返回值 
        NSLog(@"return value = %@",returnValue); 

  • 相关阅读:
    python项目---数据可视化(02)
    python项目---数据可视化(01)
    sort 快排解决百万级的排序
    插入排序专题 直接插入 折半 希尔shell
    人见人爱A^B
    内部收益率
    台球碰撞
    杭电 1061 Rightmost Digit计算N^N次方的最后一位
    数字整除
    循环 未理解
  • 原文地址:https://www.cnblogs.com/swallow37/p/4435670.html
Copyright © 2011-2022 走看看