wycats (owner)

Revisions

gist: 228727 Download_button fork
public
Public Clone URL: git://gist.github.com/228727.git
Embed All Files: show embed
mixins.m #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
 
@interface Mixin : NSObject
@end
 
@implementation Mixin
+ includeInto:(Class)cls
{
  id mixin = [[self class] copy];
  class_setSuperclass(mixin, class_getSuperclass(cls));
  class_setSuperclass(cls, mixin);
}
@end
 
@interface Bar : Mixin
NSString *thing;
@end
 
@implementation Bar
- init:(NSString *)string {
  
}
 
- bar {
  NSLog(@"Inside bar");
}
@end
 
@interface Foo : NSObject
@end
 
@implementation Foo
 
- foo {
  NSLog(@"Inside foo");
}
 
@end
 
int main(void) {
  id foo = [Foo new];
  [foo foo];
  [Bar includeInto:[Foo class]];
  [foo bar];
 
  return 0;
}