如何发布返回的对象?

时间:2011-09-10 12:16:12

标签: iphone objective-c ios ios4 nsmutabledictionary

我在一个类中编写了这个方法:

- (NSMutableDictionary *)fetch {
   NSMutableDictionary *ret = [[NSMutableDictionary alloc] init];

  // filling ret with some data here

  return ret;
}

在没有发布的情况下返回此NSMUtableDictionary对象的方法是否正确?

求助,

的Stephane

3 个答案:

答案 0 :(得分:3)

执行以下操作:

- (NSMutableDictionary *)fetch {
   NSMutableDictionary *ret = [[NSMutableDictionary alloc] init];
      // filling ret with some data here
   return [ret autorelease];
}

要详细了解autorelease,请阅读this Apple document called Memory Management Programming Guide

答案 1 :(得分:2)

像这样使用

- (NSMutableDictionary *)fetch {
   NSMutableDictionary *ret = [[NSMutableDictionary alloc] init];

  // filling ret with some data here

  return [ret autorelease];
}

答案 2 :(得分:0)

Developer Docs

自动释放将对象放入自动释放池中,并在返回后立即调用释放。