繁体   English   中英

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

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

我想将NSMUtableDictionary值复制到另一个NSMutableDictioary中。 我怎样才能做到这一点?

我的代码是,

 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.
}

我想访问person类的tempDictionary值。 那我如何从feedDictionary复制到tempDictionary。 请帮帮我。

谢谢。

这个怎么样?

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

干杯

[feedDictionary mutableCopy];

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

获取字典的深度可变副本的一种非常简单的方法是使用JSON。 它还有机会查看字典中的值

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

这是另一种保存方式。 假设您将名为“dictA”的可复制字典复制到名为“dictB”的新可变字典中。 但要确保dictB有alloc和init。

[dictB setDictionary:dictA];

希望这有用。

暂无
暂无

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

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