zoukankan      html  css  js  c++  java
  • oc消息转发:forwardInvocation、签名、参量个数、SEL 相关测试

    结论1、签名的参量类型伪造不正确会导致崩溃。

    结论二、签名个数不对可能会导致参量丢失。

    结论三:在签名配置正确的情况下,系统会将函数调用的所有信息打包到NSInvocation准备转发;

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        [self performSelector:@selector(goooo:) withObject:@"dddd" afterDelay:0];

        [self performSelector:@selector(exgoo) withObject:@"aa" withObject:@"bb"];

    }

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

    {

        //NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:"v@:@"];

        NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:"v@:f"];

        return signature;

    }

    - (void)forwardInvocation:(NSInvocation *)anInvocation

    {

        __autoreleasing id returnObj;

        [anInvocation getArgument:&returnObj atIndex:(NSInteger)2];

    //dddd

    //aa

    NSLog(NSStringFromSelector(anInvocation.selector));

    //exgoo::

    //goooo:

        NSLog(returnObj);

    }

    NSInvocation的调用有2个方法,target参数可以直接设置target属性或者设置为第一个参数,如果不设置则调用第二个方法将target传入

    // 测试
    // 构建对象 测试UILabel的setText:方法
    UILabel *myObj = [UILabel new];
    NSLog(@"invocation执行前myObj.text=%@", myObj.text);
    // 构建方法签名返回类型void编码为v,对象UILabel类型编码为@,SEL编码为:,参数类型NSString编码为@
    NSMethodSignature *myMethodSignature = [NSMethodSignature signatureWithObjCTypes:"v@:@"];
    // 构建NSInvocation
    NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:myMethodSignature];
    // 设置第1个参数
    myInvocation.target = myObj;
    // 设置第2个参数
    myInvocation.selector = @selector(setText:);
    // 设置第3个参数
    NSString *newText = @"change new text";
    [myInvocation setArgument:&newText atIndex:2];
    [myInvocation retainArguments];
    // 执行
    [myInvocation invoke];
    NSLog(@"invocation执行后myObj.text=%@", myObj.text);


    作者:不上火喝纯净水
    链接:https://www.jianshu.com/p/49151a79ac6a
    來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    Ubuntu创建用户,给予sudo权限
    Walking Robot Simulation
    python remove与del 的区别
    ML Foundation
    python any()和all()
    python3无穷大,无穷小
    eclipse maven无法下载依赖时
    eclipse自动生成spring项目时各种报错最后处理方案
    Python+Selenium+Unittest实现PO模式web自动化框架(3)
    Python+Selenium+Unittest实现PO模式web自动化框架(2)
  • 原文地址:https://www.cnblogs.com/feng9exe/p/10385812.html
Copyright © 2011-2022 走看看