ios 使用复制方法调用块目标,但崩溃了

问题描述

here is screen shot

我使用invocation call block copy,我认为它等于[block copy],但是为什么会崩溃?

@implementation MyService
    + (void)load {
        [MyService startRequest:^(id  _Nonnull responSEObject,NSError * _Nonnull error) {
            NSLog(@"%@",self);
        }];
    }
    
    + (void)startRequest:(void (^)(id responSEObject,NSError *error))object {
        
            SEL sel = @selector(copy);
            NSMethodSignature* methodSign = [object methodSignatureForSelector:sel];
            NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:methodSign];
            [invocation setSelector:sel];
    
            [invocation setTarget:object];
            [invocation invoke];
    }
@end

解决方法

由于一个未记录的功能(参见 this answer),在块上调用 NSInvocation 实际上是调用块,而不是向块对象发送消息。您提供的方法签名不是实际块调用的方法签名,因此会导致未定义的行为。