Skip to content

Instantly share code, notes, and snippets.

@zxx
Last active March 22, 2016 06:26
Show Gist options
  • Save zxx/94c951e4ccea8fd34cb4 to your computer and use it in GitHub Desktop.
Save zxx/94c951e4ccea8fd34cb4 to your computer and use it in GitHub Desktop.
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(<#Original Selector#>);
SEL swizzledSelector = @selector(<#Swizzled Selector#>);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment