Skip to content

Instantly share code, notes, and snippets.

@andriitishchenko
Last active July 3, 2018 13:43
Show Gist options
  • Save andriitishchenko/80ea48d720f24cfd5f4d9c0d525ff5fd to your computer and use it in GitHub Desktop.
Save andriitishchenko/80ea48d720f24cfd5f4d9c0d525ff5fd to your computer and use it in GitHub Desktop.
NSInvocation VS objc_msgSend
Lets call the method:
- (void)document:(BAPreviewDocument*) doc shouldClose:(BOOL) shouldClose contextInfo:(void*) contextInfo;
Example of using NSInvocation
BOOL boolValue = YES;
NSMethodSignature * methodSig = [[delegate class] instanceMethodSignatureForSelector: shouldCloseSelector];
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setTarget: delegate];
[invocation setSelector: shouldCloseSelector];
[invocation setArgument: &self atIndex: 2];
[invocation setArgument: &boolValue atIndex: 3];
[invocation setArgument: &contextInfo atIndex: 4];
[invocation invoke];
Example of using objc_msgSend
#include <objc/message.h>
void (*callback)(id, SEL, BAPreviewDocument *, BOOL, void *) = (void (*)(id, SEL, BAPreviewDocument *, BOOL, void *))objc_msgSend;
(*callback)(delegate, shouldCloseSelector, self, YES, contextInfo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment