2011-01-28 78 views
4

我想複製一個NSMUtableDictionary值到另一個NSMutableDictioary。我怎樣才能做到這一點?如何將iPhone中的NSMutableDictionary值複製到另一個NSMutableDictionary中?

這裏我的代碼,

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

我想訪問tempDictionary值Person類。所以我如何從feedDictionary複製到tempDictionary。請幫助我。

謝謝。

回答

19

這個怎麼樣?

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

乾杯

4
[feedDictionary mutableCopy]; 

這將複製字典中的所有條目,但它不會複製自定義對象,除非您實現NSCopying協議。

-1

一個非常簡單的方式來獲得一本字典的深層可變副本是使用JSON。 這也給有字典中

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

看看值的機會在這裏是另一種方式來保存。 假設您將名爲「dictA」的可變詞典複製到名爲「dictB」的新可變詞典中。但要確保dictB有alloc和init。

[dictB setDictionary:dictA]; 

希望這對您有所幫助。

相關問題