Wednesday, June 19, 2013

NSMutableDictionary removeObjectForKey example in Objective C (iOS).


NSMutableDictionary removeObjectForKey

Removes a given key and its associated value from the dictionary.

- (void)removeObjectForKey:(id)aKey

Parameters
aKey
The key to remove.

Discussion of [NSMutableDictionary removeObjectForKey]
Does nothing if aKey does not exist.

For example, assume you have an archived dictionary that records the call letters and associated frequencies of radio stations. To remove an entry for a defunct station, you could write code similar to the following:

NSMutableDictionary removeObjectForKey example.
NSMutableDictionary *stations = nil;

stations = [[NSMutableDictionary alloc]
        initWithContentsOfFile: pathToArchive];
[stations removeObjectForKey:@"KIKT"];

Example of [NSMutableDictionary removeObjectForKey].
First you need to use a NSMutableDictionnary and put this code :

[countriesToLiveInDict removeObjectForKey:@"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:@"Countries"];

NSMutableDictionary removeObjectForKey example.
[dictionary removeObjectForKey:viewerKeys[indexPath.row]];  // remove array from dictionary
[viewerKeys removeObjectAtIndex:indexPath.row]; // remove key from array of dictionary keys

End of NSMutableDictionary removeObjectForKey example article.