Skip to content

Instantly share code, notes, and snippets.

@rraallvv
Created March 28, 2015 06:00
Show Gist options
  • Save rraallvv/f25e3593fcdeee5bb116 to your computer and use it in GitHub Desktop.
Save rraallvv/f25e3593fcdeee5bb116 to your computer and use it in GitHub Desktop.
Obj-C adding methods at runtime
+ (void)initialize {
Class metaClass = objc_getMetaClass(class_getName(self));
SEL selector = @selector(description);
IMP implementation = imp_implementationWithBlock(^NSString *(){
return @">>>added";
});
Method method = class_getClassMethod(metaClass, selector);
const char *type = method_getTypeEncoding(method);
class_addMethod(metaClass, selector, implementation, type);
class_addMethod(self, selector, implementation, type);
NSLog(@"class description %@", [self description]);
NSLog(@"instance description %@", [[self new] description]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment