Last active
December 26, 2015 23:09
-
-
Save traviskirton/7228100 to your computer and use it in GitHub Desktop.
Clever, clever trick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "C4WorkSpace.h" | |
#import "ClassA.h" | |
#import "ClassB.h" | |
@implementation C4WorkSpace | |
-(void)setup { | |
ClassA *objA = [ClassA new]; | |
ClassB *objB = [ClassB new]; | |
[objA methodA]; | |
[objA methodB]; | |
[objB methodA]; | |
[objB methodB]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "C4Object.h" | |
#import "DefaultProtocol.h" | |
@interface ClassA : C4Object <DefaultProtocol> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ClassA.h" | |
@implementation ClassA | |
-(void)methodA { | |
C4Log(@"%@ | %@", NSStringFromSelector(_cmd), NSStringFromClass([self class])); | |
} | |
-(void)methodB { | |
C4Log(@"%@ | %@", NSStringFromSelector(_cmd), NSStringFromClass([self class])); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "C4Object.h" | |
#import "DefaultProtocol.h" | |
@interface ClassB : C4Object <DefaultProtocol> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ClassB.h" | |
#import "ClassA.h" | |
#import <objc/runtime.h> | |
@implementation ClassB | |
+(void)initialize { | |
Method methodA = class_getInstanceMethod([ClassA class], @selector(methodA)); | |
class_replaceMethod([self class], | |
@selector(methodA), | |
method_getImplementation(methodA), | |
method_getTypeEncoding(methodA)); | |
Method methodB = class_getInstanceMethod([ClassA class], @selector(methodB)); | |
class_replaceMethod([self class], | |
@selector(methodB), | |
method_getImplementation(methodB), | |
method_getTypeEncoding(methodB)); | |
} | |
-(void)methodA{ | |
C4Log(@"a"); | |
} | |
-(void)methodB{ | |
C4Log(@"b"); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@protocol DefaultProtocol <NSObject> | |
-(void)methodA; | |
-(void)methodB; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Output WITH implementation copying | |
--- | |
[C4Log] methodA | ClassA | |
[C4Log] methodB | ClassA | |
[C4Log] methodA | ClassB | |
[C4Log] methodB | ClassB | |
Output WITHOUT implementation copying | |
--- | |
[C4Log] methodA | ClassA | |
[C4Log] methodB | ClassA | |
[C4Log] a | |
[C4Log] b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment