...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!