Reputation: 119
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.
Upvotes: 1
Views: 2342
Reputation: 3805
An NSArray
or NSMutableArray
is an array of pointers. No copying is done.
Upvotes: 6
Reputation: 43330
...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!
Upvotes: 2
Reputation: 13459
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
By the way you should use an NSMutableArray.
Also look at the superclass 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.
Upvotes: 2