1

I dont know how to declare a array which just stores pointers to objects. As per My understanding ,if I use method

    [ someArray addObject:someObject ] ,

It would then add the copy of object to array and any changes to object wont get reflected to original object.

What I want is that create a array of pointers which would just point to objects and changes made to objects would persist. pardon me, If I am missing something basic.

user1237220
  • 119
  • 8
  • It would then add the copy of object to array and any changes to object wont get reflected to original object. - incorrect see answer you should have accepted – deleted_user Aug 10 '12 at 18:34

3 Answers3

6

An NSArray or NSMutableArray is an array of pointers. No copying is done.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
deleted_user
  • 3,817
  • 1
  • 18
  • 27
2

you have your basics wrong. technically when you do that you create the array of pointers to those objects.

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html read the description.

If you want to get the object copied you have to explicitly say so.

Look at this question for example

Deep copying an NSArray

By the way you should use an NSMutableArray.

Also look at the superclass NSArray

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/cl/NSArray

specifically for the initWithArray:copyItems:

flag

If YES, each object in array receives a copyWithZone: message to create a copy of the object—objects must conform to the NSCopying protocol. In a managed memory environment, this is instead of the retain message the object would otherwise receive. The object copy is then added to the returned array.

If NO, then in a managed memory environment each object in array simply receives a retain message when it is added to the returned array.

By default adding an object to a nsmutablearray increases its capacity if necessary, adds a retain for the object, and the pointer to the object.

Community
  • 1
  • 1
Pochi
  • 13,391
  • 3
  • 64
  • 104
2

...if I use method

[ someArray addObject:someObject ] ,

It would then add the copy of object to array and any changes to object wont get reflected to original object.

While it technically doesn't pertain to the question, I simply must correct your terminology. "Copy" in Objective-C implies that the method -copy is sent to the object, which would create a new object in of itself. What Arrays do is send -retain to their objects, which means that the array itself now owns a stake in the object, which is why changes that don't reference the array (-objectAtIndex:), or have a valid claim to the object itself are not reflected.

What I want is that create a array of pointers which would just point to objects and changes made to objects would persist. pardon me, If I am missing something basic.

Well, unfortunately iOS does not support the class NSPointerArray, which would make your life significantly easy in regards to an actual array of pointers. Without getting into any C-craziness, I can only reiterate what I mentioned above: If you need to mutate an object in an array, just access it with a valid reference to it, or use -objectAtIndex. So long as you still have a valid claim on the object (a reference in this case, it's pointer didn't change because it was sent -retain) you can change it. Note the simple example below:

NSMutableString *str = [[NSMutableString alloc]initWithString:@"Hello"];
NSArray *arr = [[NSArray alloc]initWithObjects:str, nil];
NSLog(@"%@",arr);
[str appendString:@" Friend!"];
NSLog(@"%@",arr);

This prints:

2012-08-07 21:37:46.368 .MyApp[2325:303] (
    Hello
)
2012-08-07 21:37:46.369 .MyApp[2325:303] (
    "Hello Friend!"
)

Simple!

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Why would NSPointerArray be useful for what the OP is asking? – Firoze Lafeer Aug 08 '12 at 03:42
  • 2
    the array is an actual array of pointers. the difference to the class you mention is NSPointerArray is a mutable collection modeled after NSArray but it can also hold NULL values, which can be inserted or extracted (and which contribute to the object’s count). https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPointerArray_Class/Introduction/Introduction.html – Pochi Aug 08 '12 at 03:46
  • @CodaFi, I think the behavior the OP is looking for **is** the behavior of a normal NSMutableArray. – Firoze Lafeer Aug 08 '12 at 03:49
  • Yes, exactly, which is why I included the example. Perhaps I'm being a bit too literal... – CodaFi Aug 08 '12 at 03:52