Copying the contents of an NSMutableDictionary into another NSMutableDictionary?

NSExplorer picture NSExplorer · Jan 5, 2011 · Viewed 17.5k times · Source

I have an NSMutableDictionary each element of which is another dictionary. What is the best way I can copy its contents into another NSMutableDictionary? I've tried using:

firstDictionary = [NSMutableDictionary dictionaryWithDictionary:secondDictionary];

However not sure if this is the best way to do it.

Answer

Jay Imerman picture Jay Imerman · Apr 26, 2011

You can also jump between mutable and non-mutable dictionaries using copy and mutableCopy.

- (void) someFunc:(NSMutableDictionary *)myDict {
    NSDictionary *anotherDict = [myDict copy];
    NSMutableDictionary *yetAnotherDict = [anotherDict mutableCopy];
}