Last active
November 29, 2018 05:55
-
-
Save dmaclach/ed4ec374b1da1e9f2cbe3c442723572d to your computer and use it in GitHub Desktop.
Create an Objective C Class at runtime
This file contains 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
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
Class myClass = objc_allocateClassPair([NSObject class], "MyClass", 0); | |
IMP methodIMP = imp_implementationWithBlock(^(id self) { | |
return @"Hello"; | |
}); | |
if (!class_addMethod(myClass, @selector(description), methodIMP, "@@:")) { | |
exit(1); | |
} | |
objc_registerClassPair(myClass); | |
id classInstance = [[myClass alloc] init]; | |
NSLog(@"%@", classInstance); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment