If I implement a class that has no sensible default values, what should I do with the init method which takes no arguments? If there are no valid defaults, the init method has no use.
My gut says that it should release the object and return nil:
- (id)init {
[self release];
return nil;
}
...but when reading the Apple do开发者_如何转开发cs for NSObject:
Every class must guarantee that the init method either returns a fully functional instance of the class or raises an exception.
That confuses me :(
Raise an exception. :) Something like this:
- (id)init
{
[self release];
//self = nil; not required when throwing an exception
@throw [NSException
exceptionWithName: NSGenericException
reason: @"-init not supported"
userInfo: nil];
}
精彩评论