NSArray mutableCopy creates new array but still points to old contents - ios

I have an NSMutableArray called playersArray in my singleton class which holds for my applications main datasource.
Each object of playersArray is a NSDictionary and the content is like :
{
sgfID = 1;
sgfPlayer = "<PlayerContact: 0xbf851b0>";
}
PlayerContact is a NSObject subclass containing properties like:
NSString * playerName, playerTeam, BOOL PlayerSelected and so on.
In one of my ViewControllers, in viewDidLoad, I want to take a deep copy of playersArray in to a NSMutableArray named duplicatePlayersArray. I do this by
playersArray = [[SGFHelper sharedHelpers] SGFContactsArray];
duplicatePlayersArray = [playersArray mutableCopy];
Now that I have two separate copies, I was under the impression that playersArray and duplicatePlayersArray are two totally different arrays in the memory. However I found that they are NOT!
Even if the debugger shows that they have different memory addresses, their contents have same memory addresses. So when i do this:
[((NSMutableDictionary *)[duplicatePlayersArray objectAtIndex:0]) setObject:#"333" forKey:#"sgfID"];
playersArray's dictionary at index:0 has ALSO "333" as key "sgfID" instead of "1" as it used to before the above line of code ran.
BUT, if I run the below code, only then, the two arrays start to differ
[duplicatePlayersArray replaceObjectAtIndex:0 withObject:tempDict];
Still this doesn't address my concern because the two arrays which I wanted to believe are different are still "connected". A change in one, results in the other array to change its contents.
Can you friends please show me a way to DEEP COPY the array I explained the contents of in a way where all of their contents are kept in different objects.

Use initWithArray:copyItems: to copy each entry in the array
NSMutableArray *duplicatePlayersArray = [[NSMutableArray alloc] initWithArray:playersArray copyItems:YES];

Related

Variable Assignment and string arrays Obj-c (iOS) for beginner

I created a single view app, added a label, an un-editable text view and a button, I have an array of strings. Really simply just want to click the button and change the string at random.
- (IBAction)viewNextPressed:(id)sender {
NSArray *affirmationStrings = #[
#"String 1 Pressed",
#"String 2 Pressed",
#"String 3 Pressed"
];
//Generate a random index from our array
int randomNIndex = arc4random() % [affirmationStrings count];
//Display a string from our array
self.displayAffirmationText.text = affirmationStrings[randomNIndex];
}
#end
Obviously this works fine for this example but its horribly inefficient as its generating the array each time the button is clicked. Where is the best place to store the array so its generated upon load and I can just access it when needed?
I see viewDidLoad but as a beginner I want to try to understand best practice for simple tasks. Secondly is the way I am storing strings fine for a large sample of say 500-1k+ strings?
For a relatively small number of strings, a better option would be to:
add either a property or an instance variable to your class
initialise said property or instance variable either in your init method, or, in the case of a View Controller, in viewDidLoad.
So, in the case of an instance variable:
#implementation MyViewController
{
NSArray *_affirmationStrings;
}
...
- (void)viewDidLoad
{
[super viewDidLoad];
_affirmationStrings = #[ ... list of strings ... ];
}
Then refer to it via _affirmationStrings.
In the case of a property, visible to other classes, read-only, with lazy initialization:
In .h:
#interface MyViewController : UIViewController
#property (readonly) NSArray *affirmationStrings
#end
In .m:
- (NSArray *)affirmationStrings
{
if (!_affirmationStrings)
_affirmationStrings = #[ ... list of strings ... ]
return _affirmationStrings;
}
Then refer to it via self.affirmationStrings.
There are also alternatives to make it read/write (so you can set the values from another class), or visible only within the class, etc.
If you want to handle lots of strings, you probably at the very least want to move the list outside of your code, to an external file (text file, JSON, XML, plist...). You can then either load it from there at once and keep it, or load it on demand (and forget about it once you no longer need it, hence reloading it again if you need it again).
You could also store the data in a database, either via Core Data or directly with SQLite.
It really all depends on your goals/requirements.

add new object to nsmutablearray replace all objects in array

i try to add new object to my nsmutablearray but every time it replace all object
-(void)addToStack:(Coordinate *)coord{
Coordinate*c = [[Coordinate alloc] init];
c.x=coord.x;
c.y = coord.y;
if (coord.x==0 && coord.y==0) {
c.x=coord.x+1;
[_stack addObject:c];
c.x=coord.x;
c.y=coord.y+1;
[_stack addObject:c];
c.y=coord.y;
}
}
You are not adding a new object but you are changing the old object where the reference will remain the same.
NSMutableArray addObject will not add it because it already exists in the array.
So, when trying to add a new object, first create a copy of the one that you want to change, like this:
Coordinate *newCoordinate = [Coordinate mutableCopy];
// change attributes
// add it to the array
Everybody who said that adding the same object twice deletes the first instance and replaces it, is wrong.
Arrays can contain duplicate references to the same object. However, it's more like saving the same street address in an rolodex twice. If you look up the address in the first entry, go break all the windows in that house, then go back, look up the address in the second slot in your rolodex, and drive to THAT address, you'll find the house has broken windows (Because both addresses point to the same house.)
Similarly, when you add the same object to an array twice, it's two pointers to the same object. When you change values of the object at index 0, you see those changes reflected in the object in index 1 because it's a second pointer to the same object.
Despite saying the wrong thing about what goes wrong with your code, #Shashi3456643 gave you the correct solution, which is to create a new, unique object for every entry in your array.
Make sure to initiate the array:
NSMutableArray *stack=[[NSMutableArray alloc] init];
This is because every time you init the array. Initialize the array once.
For example:
in .h
Coordinate*c;
in .m
- (void)viewDidLoad
{
c = [[Coordinate alloc] init];
}
-(void)addToStack:(Coordinate *)coord{
c.x=coord.x;
c.y = coord.y;
if (coord.x==0 && coord.y==0) {
c.x=coord.x+1;
[_stack addObject:c];
c.x=coord.x;
c.y=coord.y+1;
[_stack addObject:c];
c.y=coord.y;
}
}

Saving IOS dictionary with arrays as keys

i am working with Tapku's Calendar, and i want to save some values that will be user inputed.
But i am kind of stumbled on how i would achive this, here is the layout:
// allocate the arrays and dictionary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableArray *dateValueArray = [[NSMutableArray alloc] init];
// set array values
[dateValueArray addObject:#"first string"];
[dateValueArray addObject:#"Second string"];
// set dictionary with date as key, and array as value
[dict setObject:dateValueArray forKey:testdate];
The dictionary dict, will be the only Dictionary, but since that dictionary uses arrays for objects, i would have multiple arrays.
So, lets say there are multiple dates registerd in "dict", different keys would have to use different arrays? Sorry i am abit confused my self here.
Is there any way i can use 1 array to store all the strings associated with different dictionary keys ?
EDIT 1
Elaboration:
The whole idea is that the user can input text that are associated with dates.
I will need to store these values and i will need to store which date they are associated to.
So i have multiple values in an array, associated with 1 date in a dictionary.
And keeping in mind that i will have to store this, i would like to know how i should assign the values to the dates.
EDIT 2:
Basically what i need for the Array is something like AddObject ForKey
Edit 3
More elaboration::
Basically i want to access the values in this manner:
[date1][note1]
[date1][note2]
[date2][note1]
[date2][note2]
And the amount of values in both date and note are variable.
If I understand what you are asking about, what you want is the property of NSDictionary allKeys which is an array of all the keys in that dictionary.
Now I see your edit. You are in the right way. To perform what you are looking for, do something like this:
First, allocate your dict somewhre:
// allocate the arrays and dictionary
NSMutableDictionary *dict = [NSMutableDictionary new];
Now, everytime you get a new date with a new string, first check if there's the first string for that date. If yes, create a new array. If not, add your string inside the previously array.
NSMutableArray *valuesForDate = dict[givenDate];
if (!valuesForDate)
{
valuesForDate [NSMutableArray new];
dict[givenDate] = valuesForDate
}
[valuesForDate addObject:#"first string for dateGiven"];
[valuesForDate addObject:#"Second string for dateGiven"];
Now you can retrieve the values with something like you wanted:
NSString *test = dict[date1][0]; //first string associated for date1
NArray *allStringsForDate2 = dict[date2]; //array with all the strings for date2
You can try using NSMapTable instead of NSDictionary, which is much more flexible but also much harder to use. You'll also find it hard to find anyone knowing the answers if you have questions about NSMapTable. But you can definitely create an NSMapTable which will use pointers of objects as keys, instead of the values.

C-style array of pointers to Objective-C objects under ARC

I have a 2D array of pointers to Objective-C instances to keep track of game objects on a map grid.
Now I am transitioning my code to ARC, and Xcode pointed the error. I knew pointers to objects aren't allowed as struct members, but this one caught me (almost) off guard.
I understand the rationale behind the ARC constrains, but:
I can't afford the overhead of objective-C arrays when looking up objects in the grid, and
The objects themselves are already owned by an NSArray ivar defined in the same class that has the C-style grid as an ivar; the c-style array is only a conveniently structured shortcut. Futhermore, when objects are removed from the owning NSArray, I set the corresponding grid slot to NULL.
That is, the 2D array (grid) is just a collection of fast (but dumb) pointers to objects safely retained somewhere else (the NSArray ivar).
Is there a way to get away with this using casts? For example, define and alloc my grid as:
void*** _grid;
instead of
MyMapObjectClass*** _grid
and use (appropriately bridged) casts between void* <-> MyMapObjectClass* when setting or getting the pointers in each slot?
EDIT: So here is how I solved it
I changed the ivar declaration as described above. In addition, when setting an entry of my look-up grid, I did this:
// (Done **Only Once** at map initialization)
// _objectArray is an instance of NSMutableArray
MyMapObjectClass* mapObject = [[MyMapObjectClass alloc] init];
// ...configure map object, etc...
// Add to Obj-C array:
[_objectArray addObject:mapObject];
// Add pointer to 2D C array:
_grid[i][j] = (__bridge void*)mapObject;
When accessing the object at (x,y), I do the opposite:
MyMapObjectClass* object = (__bridge MyMapObjectClass*) _grid[x][y];
[object performSomeMethod];
// etc...
When removing the object from the map, I do this:
MyMapObjectClass* object = (__bridge MyMapObjectClass*) _grid[x][y];
[_objectArray removeObject:object];
_grid[x][y] = NULL;
Map objects are created once at the beginning of the game, and removed according to game progress. If I need to replace a map object for another, I would do this:
MyMapObjectClass* oldObject = (__bridge MyMapObjectClass*) _grid[x][y];
// (should mark as weak?)
[_objectArray removeObject:oldObject];
_grid[x][y] = NULL;
MyMapObjectClass* newObject = [[MyMapObjectClass alloc] init];
[_objectArray addObject:newObject];
_grid[x][y] = (__bridge void*)newObject;
Circumventing ARC using casts is generally a bad idea. The better way would be to disable ARC for your map.m (or break out just the lookup part into a separate class).Then do manual memory management inside it with retain / release and the C structures you like, as long as you do it correctly it will work fine and you will be able to call it from other classes, avoiding the overhead of nested NSArrays etc..

Getting objects from NSArray within another NSArray

for an iOS 5.0 application using ARC, i have an NSArray of objects that contain NSArray of other objects within it. Is it possible to extract a list of objects from inner arrays without iterating through the Array e.g. say, with NSpredicate or valueForKeyPath. To be clearer, I have:
NSArray *objtype1 contains
-id
-NSArray *imageObjs containing imageObjects
-imagetype = 1 <--1st imageObject
imageURL1
-imagetype = 2 <--2nd imageObject
imageURL2
-NSArray *objtype2
-other parameters
I need to extract the NSArray of imageType = 1 imageObjects to pass in for further processing. Is this possible? (I'm looking at NSpredicate, and valueForKeyPath, but have not found anything yet)
I think the reason you haven't found anything yet is because it's not there. You could implement your own category on NSArray to use predicates recursively. Perhaps someone else have done it already. I don't know.
Ummmmm it seems a bit unclear as to what you are doing. It looks like you have an NSArray of multiple types. And you want the imageObjs array inside it.
If you infact have an NSArray like this, it would be monumentally easier to convert it to an NSDictionary. Then you can use [dictionary valueForKey:#"Image Array"]; to get the image array out of it.
Currently, your solution to get the imageObjs array would be [objtype1 objectAtIndex:1]; then iterate over that array to use the imageObjects in it.
for(ImageObject *obj in arr) {
//do stuff
}

Resources