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

丶灬走出姿态 提交于 2019-12-13 13:26:15

问题


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.


回答1:


How about this?

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

Cheers




回答2:


[feedDictionary mutableCopy];

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




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/4827101/how-to-copy-nsmutabledictionary-values-into-the-another-nsmutabledictionary-in-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!