Copying the contents of an NSMutableDictionary into another NSMutableDictionary?

18,435

Solution 1

Check the NSDictionary initWithDictionary:copyItems: method.

It it enables deep copying of elements thru calling copyWithZone: method of item's class. You will have to take care of copying the fields yourself within the method.

Solution 2

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];
}
Share:
18,435

Related videos on Youtube

NSExplorer
Author by

NSExplorer

Updated on June 04, 2022

Comments

  • NSExplorer
    NSExplorer over 1 year

    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.