开发者

How to copy NSMutableDictionary values into the another NSMutableDictionary in iPhone?

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.


How about this?

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

Cheers


[feedDictionary mutableCopy];

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


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜