What I’m trying to do is search an array of dictionaries for a specific target dictionary, and if found, replace the actual dictionary in the original array with the target dictionary. The search algorithim works, but the copying of dictionaries doesn’t. The main line in question is the one that says:
tempDict=targetDict;
I was hoping that tempDict would be a pointer to the original dictionary from the original array, but when trying to log the author name, I get “moe” instead of “steve”.
-(void)viewDidAppear
{
[actualDictionary setObject:@"test" forKey:@"mainNote"];
[actualDictionary setObject:@"moe" forKey:@"authorName"];
[targetDictionary setObject:@"test" forKey:@"mainNote"];
[targetDictionary setObject:@"steve" forKey:@"authorName"];
[arrayOfNotes addObject:actualDictionary];
[self beginSearchWithMainArray];
}
-(void)beginSearchWithMainArray;
{
[self searchArray:arrayOfNotes forDict:targetDictionary];
}
-(void)searchArray:(NSMutableArray*)array forDict:(NSMutableDictionary*)targetDict
{
NSString *targetText=[targetDict objectForKey:@"mainNote"];
for(int i=0;i<[array count];i++)
{
NSMutableDictionary *tempDict=[array objectAtIndex:i];
NSString *possibleText=[tempDict objectForKey:@"mainNote"];
if([possibleText isEqualToString:targetText])
{
//found match, replace tempDict with targetDict
tempDict=targetDict;
NSLog(@"found match");
NSString *authorName=[[arrayOfNotes objectAtIndex:0] objectForKey:@"authorName"];
NSLog(@"%@", authorName); //should be steve
return;
}
//no match, search sub notes
...
}
}
replace
with
or
tempDict is a pointer to a NSMutableDictionary, but assign it to another instance doesn’t means change the content of the previous instance
you have to modify what “pointer point to” not the “pointer”, thats why you can use
setDictionary:to do the “assignment”