开发者

Pass an NSMutableArray or NSMutableDictionary to a method -- can you add to it?

If I have an NSMutableArray or NSMutableDictionary and pass it to a method a la:

-(void)addToThisArray:(NSMutableArray *)theMutableArray
{
    [theMutableArray addObject:@"test"];
}

and call it like:

 NSMutableArray *theMutableArrayToPass = [[NSMutableArray alloc] init];
 [theMutableArrayToPass addObject:@"2"];
 [self addToThisArray:theMutableArrayToPass];

Is this a valid way to alter an NSMutableArray or NSMutableDictionary? I, of course, would do much more to the array/dictionary than this example, b开发者_开发知识库ut I am used to Perl, and we pass by reference all the time.


Yes, objective-c is also always pass by reference for objects.

Since this answer was written, there is now an exception though it is an implementation detail and doesn't change the practical details of the claim.

Tagged pointers are technically pass by value because the reference is really the data. They are also, by definition, immutable which is why this is an implementation detail that has not practical ramifications.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜