Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Created March 28, 2015 11:36
Show Gist options
  • Save Air-Craft/56404a88aa82cd949f43 to your computer and use it in GitHub Desktop.
Save Air-Craft/56404a88aa82cd949f43 to your computer and use it in GitHub Desktop.
Mixin class method in Objective-C #objective-c #mixins #runtime #advanced
// 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