释放方法返回的对象

释放方法返回的对象

问题描述:

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

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