Created
March 28, 2015 06:00
-
-
Save rraallvv/f25e3593fcdeee5bb116 to your computer and use it in GitHub Desktop.
Obj-C adding methods at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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