Given an NSMutableDictionary *dict, is this a bad way to replace keys with a new name? Is there an easier way?
NSArray *originalField = [NSArray arrayWithObjects:@"oldkey", @"oldkey2", nil];
NSArray *replacedField = [NSArray arrayWithObjects:@"newkey", @"newkey2", nil];
for (int i=0; i<[originalField count]; ++i)
{
if ([dict objectForKey:[originalField objectAtIndex:i]] != nil) {
[dict setObject:[dict objectForKey:[originalField objectAtIndex:i]] forKey:[replacedField objectAtIndex:i]];
[dict removeObjectForKey:[originalField objectAtIndex:i]];
}
}
Thanks!
Nope, that’s pretty much it. In general, you’d use fast enumeration and/or NSEnumerator to walk the arrays instead of going index-by-index, but since you’re walking two parallel arrays, indexes are the clearest way to do it.