Last active
January 3, 2016 09:29
-
-
Save SpacyRicochet/8442547 to your computer and use it in GitHub Desktop.
Instead of overriding class initializers, consider overriding the new methods. There's only one default 'new' method, which makes autocompletion of your own custom ones a lot easier.
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
/** A person, with a name. */ | |
@interface Person : NSObject | |
/** Creates a new person with the specified name. */ | |
+ (id)newWithString:(NSString *)name; | |
@end |
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
@implementation Person | |
{ | |
NSString *_name; | |
} | |
+ (id)newWithString:(NSString *)name | |
{ | |
return [[self alloc] initWithName:name]; | |
} | |
- (id)initWithName:(NSString *)name | |
{ | |
NSParameterAssert(name); | |
self = [super init]; | |
if (self) { | |
_name = name; | |
} | |
return self; | |
} | |
- (id)init | |
{ | |
// Asserting this to always fail also makes a lot of sense. | |
// Might do that in the future. | |
NSLog(@"Don't use the default initializer. Use +newWithName: instead."); | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment