- (id)init
{
if (![super init])
return nil;
firstNumber = random() % 100 + 1;
secondNumber = random() % 100 + 1;
return self;
}
This version will always work and is considered the most correct form; however, none of the classes that you will subclass in this book require these checks. For simplicity, this book will sometimes leave out the check.
Conventions for Creating Initializers
Following are rules that Cocoa programmers try to follow regarding initializers.
You do not have to create any initializer in your class if the superclass's initializers are sufficient.
If you decide to create an initializer, you must override the superclass's designated initializer.
If you create multiple initializers, only one does the work—the designated initializer. All other initializers call the designated initializer.
The designated initializer of your class will call its superclass's designated initializer.
The day will come when you will create a class that must, must, must have an argument supplied. Override the superclass's designated initializer to throw an exception:
- (id)init
{
[self dealloc];
@throw [NSException exceptionWithName:@"BNRBadInitCall"
reason:@"Initialize Lawsuit with initWithDefendant:"
userInfo:nil];
return nil;
}
No comments:
Post a Comment