Skip to content

Instantly share code, notes, and snippets.

@madhavan-rp
Last active August 29, 2015 13:56
Show Gist options
  • Save madhavan-rp/8858492 to your computer and use it in GitHub Desktop.
Save madhavan-rp/8858492 to your computer and use it in GitHub Desktop.
ViewController+Swizzle
//
// ViewController+Swizzle.h
// TryingOutStuff
//
#import "ViewController.h"
@interface ViewController (Swizzle)
@end
//
// ViewController+Swizzle.m
// TryingOutStuff
//
#import "ViewController+Swizzle.h"
#import <objc/runtime.h>
@implementation ViewController (Swizzle)
- (void) swizzle_viewDidLoad {
NSLog(@"I am swizzling view Did Load!!");
[self swizzle_viewDidLoad];
IMP instanceForDoSomething = [ViewController instanceMethodForSelector:@selector(dosSomethingWith:)];
void (*func)(id,SEL,...) = (void *)instanceForDoSomething;
func(self,@selector(dosSomethingWith:),@"this thing");
}
+ (void) load {
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(viewDidLoad));
swizzled = class_getInstanceMethod(self, @selector(swizzle_viewDidLoad));
method_exchangeImplementations(original, swizzled);
}
-(void) dosSomethingWith:(NSString *)message {
NSLog(@"Doing something with : %@",message);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment