简体   繁体   English

如何将NSMutableDictionary值复制到iPhone中的另一个NSMutableDictionary?

[英]How to copy NSMutableDictionary values into the another NSMutableDictionary in iPhone?

I want to copy a NSMUtableDictionary values into the another NSMutableDictioary. 我想将NSMUtableDictionary值复制到另一个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. 我想访问person类的tempDictionary值。 SO how can i copy from feedDictionary to the tempDictionary. 那我如何从feedDictionary复制到tempDictionary。 Please help me out. 请帮帮我。

Thanks. 谢谢。

How about this? 这个怎么样?

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

Cheers 干杯

[feedDictionary mutableCopy];

这将复制字典中的所有条目,但除非您实施NSCopying协议,否则它不会复制自定义对象。

a very simple way to get a deep mutable copy of a dictionary is to use JSON. 获取字典的深度可变副本的一种非常简单的方法是使用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". 假设您将名为“dictA”的可复制字典复制到名为“dictB”的新可变字典中。 but make sure dictB have alloc and init. 但要确保dictB有alloc和init。

[dictB setDictionary:dictA];

Hope this helpful. 希望这有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM