Skip to content

Instantly share code, notes, and snippets.

@PavelGnatyuk
Created July 28, 2013 07:45
Show Gist options
  • Save PavelGnatyuk/6097854 to your computer and use it in GitHub Desktop.
Save PavelGnatyuk/6097854 to your computer and use it in GitHub Desktop.
Objective-C run-time is used to add a new method to an existing class.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *temp = @"world";
int (^impyBlock)(id, int, int) = ^(id _self, int a, int b)
{
return a + b;
};
int (*impyFunct)(id, SEL, int, int) = (void*) imp_implementationWithBlock(impyBlock);
class_addMethod([temp class], @selector(test:param:), (IMP)impyFunct, "i@:ii");
int result = [temp test:1 param:2];
NSLog(@"result = %i", result);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment