Skip to content

Instantly share code, notes, and snippets.

@SpacyRicochet
Last active January 3, 2016 09:29
Show Gist options
  • Save SpacyRicochet/8442547 to your computer and use it in GitHub Desktop.
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.
/** A person, with a name. */
@interface Person : NSObject
/** Creates a new person with the specified name. */
+ (id)newWithString:(NSString *)name;
@end
@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