iOS笔记面试常见问题

崩溃预防处理 消息转发

2021-01-21  本文已影响0人  狗蛋的春天

新建NSObject分类

  - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
      //方法签名
      return  [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
 - (void)forwardInvocation:(NSInvocation *)anInvocation{
          NSLog(@"crash-----类:%@中 未实现该方法",NSStringFromClass([anInvocation.target             class]),NSStringFromSelector(anInvocation.selector));
    }

如下情况不会崩溃,会走消息转发机制打印信息

    UIButton *bbb = [UIButton buttonWithType:UIButtonTypeCustom];
    [bbb setTitle:@"xxxx" forState:UIControlStateNormal];
    bbb.frame = CGRectMake(100, 100, 100, 40);
    bbb.backgroundColor = [UIColor redColor];
    [self.view addSubview:bbb];
    [bbb addTarget:self action:@selector(xxxx) forControlEvents:UIControlEventTouchUpInside];

    [[NSObject new] performSelector:@selector(ffff)];
上一篇 下一篇

猜你喜欢

热点阅读