[self release],[self dealloc]或[super dealloc]在init方法? [英] [self release], [self dealloc] or [super dealloc] in init methods?

查看:127
本文介绍了[self release],[self dealloc]或[super dealloc]在init方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了如何在init方法中正确失败,文档似乎彼此不同意。一个建议抛出异常,而其他人建议清理和返回零。

I've just been reading up on how to properly fail in an init method and the docs seem to disagree with each other. One recommends throwing an exception while the others recommend cleaning up and returning nil. What's the current best practice here?

推荐答案

我相信普遍接受的做法是在失败时返回nil。但你确实想要释放自我以避免泄漏:

I believe that the generally accepted practice is to return nil on failure. But you do want to release self to avoid a leak:

-(id)init
{
  if (self = [super init]) {
    ...
    if (thingsWentWrong) {
      [self release];
      return nil;
    }
    ...
  }
  return self;
}

这篇关于[self release],[self dealloc]或[super dealloc]在init方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆