Created
March 28, 2015 11:36
-
-
Save Air-Craft/56404a88aa82cd949f43 to your computer and use it in GitHub Desktop.
Mixin class method in Objective-C #objective-c #mixins #runtime #advanced
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
// MIXINS | |
+ (void)mixInto:(Class)destCls | |
{ | |
NSAssert([[destCls alloc] isKindOfClass:[UIViewController class]], @"%@ is not a UIViewController subclass", destCls); | |
void (^inject)(Class, SEL, Class) = ^(Class srcCls, | |
SEL sel, | |
Class destcls){ | |
Method newMethod = class_getInstanceMethod(srcCls, sel); | |
class_replaceMethod(destCls, sel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); // replace is like force-add, where addMethod fails if exists | |
}; | |
inject(self, @selector(prevKeyWindow), destCls); | |
inject(self, @selector(setPrevKeyWindow:), destCls); | |
inject(self, @selector(modalWindow), destCls); | |
inject(self, @selector(setModalWindow:), destCls); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment