Skip to content

Instantly share code, notes, and snippets.

@tternes
Created April 14, 2014 19:58
Show Gist options
  • Save tternes/10678320 to your computer and use it in GitHub Desktop.
Save tternes/10678320 to your computer and use it in GitHub Desktop.
ARC + Retain Hack
- (id)swizzled_retain
{
NSLog(@"yes, I know");
return [self swizzled_retain];
}
+ (void)load
{
// swizzle retain
Class c = [self class];
SEL originalSelector = NSSelectorFromString(@"retain");
SEL newSelector = @selector(swizzled_retain);
Method origMethod = class_getInstanceMethod(c, originalSelector);
Method newMethod = class_getInstanceMethod(c, newSelector);
if(class_addMethod(c, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, newSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment