释放方法返回的对象 [英] Releasing objects returned by method

查看:70
本文介绍了释放方法返回的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道这个问题的答案应该很明显,但是我需要朝正确的方向稍加推动.

Ok, I know the answer to this question should be obvious, but I need a little push in the right direction.

我发现自己编写了许多遵循以下模式的方法:

I find myself writing a fair number of methods that follow the following pattern:

-(NSThing*)myMethod{

  NSThing *thing = [[NSthing alloc] init];
  // do some stuff with the thing
  return thing;
}

我的问题是,如何处理该对象的释放?显然,我无法在方法中释放它.

My question is, how do I handle the release of this object? Clearly I can't release it within the method.

推荐答案

通常您会自动释放它

-(NSThing*)myMethod{

  NSThing *thing = [[NSthing alloc] init];
  // do some stuff with the thing
  return [thing autorelease];
}

这篇关于释放方法返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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