Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (C)2011 to3oo mi2ukam1 tmiz.net
- //
- // interposing on objc sample
- // $ g++ main.m -framework cocoa
- // $ ./a.out
- // 2011-12-20 12:12:37.159 a.out[533:903] hello
- // 2011-12-20 12:12:37.164 a.out[533:903] world
- #include <cocoa/cocoa.h>
- #include <objc/objc-class.h>
- // --- example instead of existing class
- @interface AObject : NSObject {
- };
- -(void)sayHello;
- @end
- @implementation AObject
- -(void)sayHello
- {
- NSLog(@"hello");
- }
- @end
- //
- @implementation AObject(DYNA)
- -(void)sayHello_new
- {
- [self sayHello_new];
- NSLog(@"world");
- }
- @end
- void interpose()
- {
- Class aobj_class = objc_getClass("AObject");
- Method a = class_getInstanceMethod(aobj_class,
- @selector(sayHello));
- Method b = class_getInstanceMethod(aobj_class,
- @selector(sayHello_new));
- method_exchangeImplementations(a, b);
- }
- int main() {
- interpose();
- id aobj = [[AObject alloc] init];
- [aobj sayHello];
- [aobj release];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment