Skip to content

Instantly share code, notes, and snippets.

@rgoulter
Last active August 29, 2015 13:56
Show Gist options
  • Save rgoulter/8800493 to your computer and use it in GitHub Desktop.
Save rgoulter/8800493 to your computer and use it in GitHub Desktop.
Obj-C, Constructors and Overloading
#import <stdio.h>
#import <Foundation/Foundation.h>
@interface Foo : NSObject
- (void)foo;
@end
@implementation Foo
+ (void) initialize {
if (self == [Foo class]) {
printf("Foo +initialize\n");
}
}
- (id) init {
self = [super init];
if (self) {
[self foo];
}
return self;
}
- (void) foo {
printf("hello\n");
}
@end
@interface Bar : Foo
@end
@implementation Bar
+ (void) initialize {
if (self == [Bar class]) {
printf("Bar +initialize\n");
}
}
- (id) init {
self = [super init];
if (self) {
printf("Bar initializer\n");
[self foo];
}
return self;
}
- (void) foo {
printf("baa\n");
}
@end
int main( int argc, const char *argv[] ) {
id foo = [[Bar alloc] init];
return 0;
}
Foo +initialize
Bar +initialize
baa
Bar initializer
baa
@rgoulter
Copy link
Author

Oops, + (void) initialize is only for class-level initialization, so might not be at all useful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment