views:

45

answers:

1

Okay, coming from a perl background I am used to Arrays holding refs to Hashes (Dictionary in iPhone parlance).

What I would like to know is this: If I have a NSArray of NSMutableDictionaries. Say I added the following NSMutableDictionary to an NSArray:

[theArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: timestamp, @"timestamp" ,name, @"name" ,lat, @"lat" ,lon, @"lon" , nil]];

Then later I cycled through the array like so:

for (NSMutableDictionary theDict in theArray)
{

}

does each theDict represent a pointer NSMutableDictionary's data such that I can do this:

for (NSMutableDictionary *theDict in theArray)
{
      if ([theDict objectForKey:@"timestamp"] != nil && [[theDict objectForKey:@"timestamp"] isEqualToString:@"timestamp"])
      {
           [theDict setObject:@"new name" forKey@"name"];
      }   
}

and then expect theArray to hold the changed value throughout the life of theArray?

or, do I need to do this:

int ct = 0;
for (NSMutableDictionary *theDict in theArray)
{
      if ([theDict objectForKey:@"name"] != nil && [[theDict objectForKey:@"name"] isEqualToString:@"name you want to change"])
      {
           NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] initWithDictionary:theDict copyItems:YES];
           [tmpDict setObject:@"new name" forKey@"name"];
           [theDict replaceObjectAtIndex:ct withObject:tmpDict];
           [tmpDict release];
           break;
      }
      ct += 1;
}

It cannot be all that code, can it? -- just to replace one value for a key of a NSMutableDictionary held in an NSArray?

Sorry if this question is obvious to some...but still trying to wrap my mind around pointer to a dictionary (or object) in Objective-c -- with is roughly equivalent to a ref to a hash in perl -- vs a copy (or deepcopy) of an object in Objective-c. And the problem is made more confusing by the NSArray holding the pointers (hopefully) to the NSMutableDictionaries. This is how it is done in perl -- and from what I am understanding -- outside of the name (pointer vs reference) .. it is the same in Objective-c.

+1  A: 

You should do it the first way. Each theDict is a pointer to the original, not a copy.

Edit: remainder of answer no longer relevant following edit of question, but left around to give context to comments and because you can never have too many links to the object ownership docs!


Your autorelease vs release aside suggests a possible misunderstanding about something else, but this perhaps isn't the time to go into that in detail. I suggest you have another look at the object ownership docs to be sure you understand what autorelease really means.

(Btw, your initial tmpArray should be theArray I think, unless I've totally misunderstood your question. And the type of the loop variables should be NSMutableDictionary* rather than NSMutableDictionary.)

walkytalky
I think it's always the place to address memory management misunderstandings. Better educate people early and often on the fundamentals (and I'm just a novice myself).
Joost Schuur
Ha. Was just adding a link while you were posting that :)
walkytalky
autorelease vs release was not what I meant. I just meant that will the change "stick" after the [tmpDict release]; or not? (since I am not using the 2nd way, it does not matter). I was trying to cover so many things in the post that I just did not read it fully before hitting the submit button. Is it permissible to edit the post to remove that line ("according to the NSMutableDictionary's original autorelease vs release directive") or should i leave it in?
Jann
@Jann I don't think there's any hard and fast etiquette, though I guess it would be bad form to completely undermine people's answers. But by all means edit in this case, since it's a side issue anyway.
walkytalky
Edited. Thanks for all the help. So, to summon it up, my thought that the "theDict" I get from "theArray" is just like a reference to a Hash that I get from an Array in perl...in that they are both pointers to the actual data in memory and not copies that I must then put back after I get done editing them. I know it is not good to think of "does this in O-c" mean "that like it does in Perl" ... but sometimes it is the only way I can think of to get a question across. Thanks! Checked-boxed as the answer and clicked answer as "useful".
Jann