How to copy NSMutableDictionary values into the another NSMutableDictionary in iPhone?
Asked Answered
B

4

4

I want to copy a NSMUtableDictionary values into the another NSMutableDictioary. How can i do that?

Here my code is,

 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 to the person class. SO how can i copy from feedDictionary to the tempDictionary. Please help me out.

Thanks.

Bramblett answered 28/1, 2011 at 10:20 Comment(0)
L
19

How about this?

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

Cheers

Loveinidleness answered 28/1, 2011 at 10:31 Comment(0)
P
5
[feedDictionary mutableCopy];

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

Pratique answered 28/1, 2011 at 10:29 Comment(0)
A
-1

a very simple way to get a deep mutable copy of a dictionary is to use JSON. It also gives the chance to have a look at values in dictionaries

NSString *dictStr = [sourceMutableDict JSONRepresentation];
// NSLog(@"copied data dict: %@", dictStr);
copyMutableDict = [[dictStr JSONValue] retain];
Assamese answered 13/7, 2012 at 12:2 Comment(0)
J
-1

Here is another way to save. Suppose you have copy mutable dictionary named "dictA" into new mutable dictionary named "dictB". but make sure dictB have alloc and init.

[dictB setDictionary:dictA];

Hope this helpful.

Jemima answered 7/9, 2012 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.