Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing a key name in NSDictionary

I have a method which returns me a nsdictionary with certain keys and values. i need to change the key names from the dictionary to a new key name but the values need to be same for that key,but i am stuck here.need help

like image 436
iphone66 Avatar asked Apr 12 '11 08:04

iphone66


1 Answers

This method will only work with a mutable dictionary. It doesn't check what should be done if the new key already exists.

You can get a mutable dictionary of a immutable by calling mutableCopy on it.

- (void)exchangeKey:(NSString *)aKey withKey:(NSString *)aNewKey inMutableDictionary:(NSMutableDictionary *)aDict
{
    if (![aKey isEqualToString:aNewKey]) {
        id objectToPreserve = [aDict objectForKey:aKey];
        [aDict setObject:objectToPreserve forKey:aNewKey];
        [aDict removeObjectForKey:aKey];
    }
}
like image 81
Nick Weaver Avatar answered Sep 28 '22 01:09

Nick Weaver