开发者

What should I do with the no-arguments init method if there are not default values?

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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜