簡體   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