2011-01-05 10 views
0

Wie kann man den Inhalt eines NSMutableDictionary in ein anderes NSMutableDictionary kopieren?Deep copy von NSMutableDictionary

Ich möchte nicht initWithDictionary: copyItems: Methode verwenden, da meine Wörterbücher bereits initialisiert sind.

Antwort

0

Dies könnte funktionieren:

- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary 

Dadurch werden die Schlüssel, aber nicht die Werte, eines NSDictionary. Natürlich, wenn Sie eine tiefe Kopie benötigen, werfen Sie einen Blick auf diese:

@interface NSDictionary(rross) 

- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL) copyItems; 

@end 

@implementation NSDictionary(rross) 

- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL) copyItems 
{ 
    if (copyItems) 
    { 
     // get the keys 
     NSArray *oldKeys = [otherDictionary allKeys]; 

     for (int i = 0; i < oldKeys.count; i++) 
     { 
       // add all the new key/value pairs 
       id key = [oldKeys objectAtIndex:i]; 
       [self setObject:[[[otherDictionary objectForKey:key] copy] autorelease] forKey:[[key copy] autorelease]]; 
     } 
    } 
    else 
    { 
     [self addEntriesFromOtherDictionary:otherDictionary]; 
    } 
} 

@end 
Verwandte Themen