Created
October 15, 2013 20:50
-
-
Save hborders/6998448 to your computer and use it in GitHub Desktop.
Demonstrates problem with static methods
This file contains hidden or 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
@interface Foo : NSObject | |
+ (void)bar; | |
@end | |
@implementation Foo | |
+ (void)bar { | |
} | |
@end | |
@interface Baz : NSObject | |
@end | |
@implementation Baz | |
+ (void)initialize { | |
if (self == [Baz class]) { | |
[self bar]; // No known class method for selector 'bar'. Yay! | |
} | |
} | |
- (id)init { | |
self = [super init]; | |
if (self) { | |
[[self class] bar]; // No errors. Boo! | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are at least two code smells here:
self
in-init
and+initialize
is fragile due to the potential for side effects on an object that's not fully initialized.Baz
was subclassed,+bar
may not be what you want anymore. The call at :30 should explicitly refer toBaz
instead of[self class]
.