How to work with NSMutableArray

I am trying to edit an NSMutableArray to place into a dictionary.



        aFinalized [1][@"round_number"]         = [[NSString alloc] initWithFormat:@"%i",aRound[1]]; // which is #1

I do the NSLog and it shows that I have 7 different Objects, but when I change the obect for aFinalaized [1] as above. It changes it for ALL of them. I don't get it?



Example: Prior to code above...

2015-09-07 17:04:14.575 Poker Setup & Timer[217:5819] 
 aFinalizedR= (
        {
        "round_number" = 0;
    },
        {
        "round_number" = 0;
    },
        {
        "round_number" = 0;
    },etc...


Example: After code Above...

2015-09-07 17:05:48.317 Poker Setup & Timer[217:5819] 
 aFinalizedR= (
        {
        "round_number" = 1;
    },
        {
        "round_number" = 1;
    },
        {
        "round_number" = 1;
    }, etc...


Please Help, this seems so easy, but I've been trying differen't things all day!

You mentioned placing the array in a dictionary, but your code has it the other way around. Did you make a typo or are you actually trying to put the array into the dictionary?

Hmmm. So what I am trying to do is compile an array to write to the dictionary. So what did you see that I probably did backwards. Because there's a good chance you are right?

"aRound" is an array of numbers, "aFinalized" is a NSMutableArray that I was trying to put into a dictionary.

My guess would be that you initialized your array something like this:


NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
// fill out the default members of 'dictionary'
for (int i = 0; i < numberOfEntries; ++i)
     [array addObject:dictionary]


This will give you an array with numberOfEntries entries that all point at the same dictionary, and thus when you change any dictionary you change all of them.


Minimally, you need to create a mutableCopy of the original dictionary for each entry in the array, or to rearrange the creation to add newly allocated dictionaries for each entry.

But he said he's trying to place the array into the dictionary.

You said you're trying to place the array into the dictionary, like this:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
dictionary[@"round_number"] = <#wherever your array is from#>;


Now to modify an element in the array, you would use a line of code like this:

aFinalized[@"round_number"][1] = [[NSString alloc] initWithFormat:@"%i",aRound[1]]; // which is #1


All I did was reverse the order of the subscripts. So now your line of code does the following:

•Accesses the dictionary's "round_number" member, which is an array

•Accesses the array's member 1 and sets it to the format


Do you see how the order of the subscripts matters?



One more thing… your aRound array is a C-style array of primitive number types, right? The format marker %i won't work with NSNumber.

If he did the same thing the other way around, he would have the same problem – I was simply trying to explain what might cause what he was immediately seeing.

That's true. Fair enough.

How to work with NSMutableArray
 
 
Q