1

here is screen shot

I use invocation call block copy, I think it's equals to [block copy],but crashed why?

@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
newacct
  • 119,665
  • 29
  • 163
  • 224
Y Robin
  • 80
  • 1
  • 6

1 Answers1

0

Due to an undocumented feature (see this answer), invoking NSInvocation on a block actually calls the block, instead of sending a message to the block object. The method signature you provided is not the method signature of the actual block call, so it leads to undefined behavior.

newacct
  • 119,665
  • 29
  • 163
  • 224