How to copy NSMutableDictionary values ​​to another NSMutableDictionary in iPhone?

I want to copy the values ​​of NSMUtableDictionary to another NSMutableDictioary. How can i do this?

Here is my code,

PersonClass.h file: NSMutableDictionary *tempDictionary; @property (nonatomic, retain) NSMutableDictionary *tempDictionary; PersonClass.m @synthesize tempDictionary; tempDictionary = [[NSMutableDictionary alloc] init]; -(PersonClass *) initWithPerson:(NSMutableDictionary *) feedDictionary { // get all the values in feedDictionary. //Now i want copy of the details from feedDictionary into the tempDictionary tempDictionary = feedDictionary; // Doesn't copy. } 

I want to access the tempDictionary values ​​for the person class. SO how can I copy from feedDictionary to tempDictionary. Please help me.

Thanks.

+4
iphone nsmutabledictionary
source share
4 answers

How about this?

 tempDictionary = [[NSMutableDictionary alloc]initWithDictionary:feedDictionary]; 

Greetings

+19
source share
 [feedDictionary mutableCopy]; 

This will copy all entries in the dictionary, but will not copy user objects unless you implement the NSCopying protocol.

+4
source share

A very easy way to get a deep mutable copy of a dictionary is to use JSON. It also gives you the opportunity to look at the meanings in dictionaries.

 NSString *dictStr = [sourceMutableDict JSONRepresentation]; // NSLog(@"copied data dict: %@", dictStr); copyMutableDict = [[dictStr JSONValue] retain]; 
-one
source share

Here is another way to save. Suppose you have a copyable mutable dictionary named "dictA" in a new mutable dictionary named "dictB". but make sure dictB has alloc and init.

 [dictB setDictionary:dictA]; 

Hope this helps.

-one
source share

All Articles