0
如何將NSMutableDictionary的內容深層拷貝到另一個NSMutableDictionary中?NSMutableDictionary的深層副本
我不想使用initWithDictionary:copyItems:方法,因爲我的字典已經初始化。
如何將NSMutableDictionary的內容深層拷貝到另一個NSMutableDictionary中?NSMutableDictionary的深層副本
我不想使用initWithDictionary:copyItems:方法,因爲我的字典已經初始化。
這可能會實現:
- (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