Y Robin
Y Robin

Reputation: 80

ios invoke block target with copy method,but crashed

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

Upvotes: 1

Views: 232

Answers (1)

newacct
newacct

Reputation: 122518

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.

Upvotes: 0

Related Questions