2011-01-05 110 views

回答

0

這可能會實現:

- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary 

此副本鑰匙,而不是值的NSDictionary的。當然,如果您需要深度複製,請看看這個:

@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