开发者

return a copy or a mutable object

Which variant is more correctly?

//first variant
    - (NSArray*) someArray
    {
     NSMutableArray* mutArr = [[NSMutableArray alloc] init];

开发者_开发技巧     //...some operations with mutArr


     NSArray* retArray = [mutArr copy];

     [mutArr release]; //remove mutArr

     return [retArray autorelease];
    }

//second variant
    - (NSArray*) someArray
    {
     NSMutableArray* mutArr = [[NSMutableArray alloc] init];

     //...some operations with mutArr 

     return (NSArray*)[mutArr autorelease];
    }


The answer is, how much of a problem will there be if the array is changed after you return it?

If you are creating a mutable array inside your method then returning it, never to use it again, I think it is fine to return the mutable version. The fact that your method declares a return type of NSArray only means you won't guarantee the array will be mutable. You don't have to guarantee that it is immutable.

On the other hand, if you are returning an array that your class uses internally, it is much safer to return an immutable copy. In your example above, that does not appear to be the case.

The consumer of the array, should they want to keep a reference, should use copy instead of retain; if the array is mutable, they will get an immutable copy. If it is already immutable, only the reference count will be increased. So there's no penalty for copying an immutable object.

In other words, your second variant is fine. (Although the cast to (NSArray *) is totally unnecessary.)


The first one is better, in my opinion. It ensures immutability.


I'm assuming that you mean for mutArr and names to be the same array. If that's the case, then the first is more correct, since you don't really need to make a copy of it.

You can just return mutArray if you want; since NSMutableArray is a subclass of NSArray, returning one will work. If you want to return a regular, non-mutable NSArray, I would recommend the following:

(NSArray*)someArray {
    NSMutableArray* mutArr = [[[NSMutableArray alloc] init ] autorelease];
    // your operations here
    return [NSArray arrayWithArray:mutArr];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜