Matt Douhan
Matt Douhan

Reputation: 2113

How do I rename a key in an NSDictionary inside an NSArray

I have a key/value pair in an NSDictionary inside an NSArray like

foo=bar

I need to rename foo in each NSDictionary inside the NSArray so that they all come out as:

jongel=bar

I have read some documentation how to extract the keys using the allKeys method but I cannot find anything about renaming a key in an NSDictionary.

Upvotes: 0

Views: 541

Answers (5)

user6788419
user6788419

Reputation: 7340

Modifying of an NSDictionary is not possible. You can try this way

NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for (int j=0; j<yourArray.count; j++) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:[yourArray objectAtIndex:j]];

        [dict setObject: [dict objectForKey: @"oldKey"] forKey: @"newKey"];
        [dict removeObjectForKey: @"oldKey"];

        [tempArray addObject:dict];
    }
yourArray = [[NSArray alloc]initWithArray:(NSArray *)tempArray];

Upvotes: 0

danh
danh

Reputation: 62676

It's more of a replace than a rename. Here's a solution that handles the mutability issue and returns a dictionary like the original...

- (NSDictionary *)changeKey:(NSString *)key toKey:(NSString *)newKey inDictionary:(NSDictionary *)d {
    NSMutableDictionary *result = [d mutableCopy];
    result[newKey] = d[key];
    [result removeObjectForKey:key];
    return result;
}

// elsewhere, call it...
NSDictionary *d = @{ /* your original immutable dictionary */ };
d = [self changeKey:@"foo" toKey:@"jongel" inDictionary:d];

This is a candidate for a dictionary extension if you use it a lot.

If it's in an immutable array, that must be mutable, too...

NSArray *myArray = ...
NSMutableArray *myMutableArray = [myArray mutableCopy];

NSDictionary *d = myArray[someIndex];
myMutableArray[someIndex] = [self changeKey:@"foo" toKey:@"jongel" inDictionary:d];
myArray = myMutableArray;

Upvotes: 3

TheHungryCub
TheHungryCub

Reputation: 1960

You can't change anything in an NSDictionary, since it is read only.

You Only do that changes in NSMutableDictionary with the new key names.

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

using

- (void)exchangeKey:(NSString *)foo withKey:(NSString *)jongel inMutableDictionary:(NSMutableDictionary *)aDict
{
//do your code
}

Upvotes: 0

Fogmeister
Fogmeister

Reputation: 77641

You can't rename a key. But you can set a new key.

If you have a mutable dictionary then you can do...

dictionary[@"jongel"] = dictionary[@"foo"];
dictionary[@"foo"] = nil;

Upvotes: 0

vadian
vadian

Reputation: 285064

First of all you need an NSMutableDictionary to do this.

If oldKey and newKey are known then there are three steps:

NSString *oldKey = @"foo";
NSString *newKey = @"jongel";

// get the value
id value = dictionary[oldKey];
// remove the old key
[dictionary removeObjectForKey:oldKey];
// set the new key
dictionary[newKey] = value;

Upvotes: 0

Related Questions