开发者

Objective C, Return an Array

 -(NSArray*)getNeighbors{
      NSArray *values = [neighbo开发者_如何学编程rs allValues];//neighbors is NSMutableDictionary
      return values;
 }

I have a very simple code like this but I am not sure that I need to add "retain"...


You don't. The method that calls getNeighbors should take care of retaining the returned array.


It depends on what you're going to do with returned value. -[NSArray allValues] returns new array, which is autoreleased, so if you're planning to use that array for a long time (outside the function scope) then you should retain it (but not in -(NSArray*)getNeighbors). Retain array (if needed outside):

-(void) foo {
    ivar_array = [[someObject getNeighbors] retain];
}

OR the case when you don't need to retain:

-(void) foo {
   NSArray* array= [someObject getNeighbors];
   //do something with that array
}


You can send retain and release to every object inherited from NSObject, depending on your needs. But in your case values is an object which you don't own, and you should take special care of how you handle it. For more information, consult Object Ownership and Disposal


You don't. This page(1) has detailed explanation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜