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

发布于 2024-10-15 06:35:47 字数 707 浏览 4 评论 0原文

我想将 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。请帮帮我。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等待圉鍢 2024-10-22 06:35:47

这个怎么样?

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

干杯

How about this?

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

Cheers

大姐,你呐 2024-10-22 06:35:47
[feedDictionary mutableCopy];

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

[feedDictionary mutableCopy];

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

月寒剑心 2024-10-22 06:35:47

获取字典的深层可变副本的一个非常简单的方法是使用 JSON。
它还提供了查看字典中的值的机会

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

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];
红墙和绿瓦 2024-10-22 06:35:47

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

[dictB setDictionary:dictA];

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文