Last active
August 29, 2015 13:56
-
-
Save rgoulter/8800493 to your computer and use it in GitHub Desktop.
Obj-C, Constructors and Overloading
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 <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; | |
} |
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
Foo +initialize | |
Bar +initialize | |
baa | |
Bar initializer | |
baa |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops, + (void) initialize is only for class-level initialization, so might not be at all useful here.