Line data Source code
1 0 : /* 2 : 3 : NSMutableDictionaryOOExtensions.m 4 : 5 : Oolite 6 : Copyright (C) 2004-2013 Giles C Williams and contributors 7 : 8 : This program is free software; you can redistribute it and/or 9 : modify it under the terms of the GNU General Public License 10 : as published by the Free Software Foundation; either version 2 11 : of the License, or (at your option) any later version. 12 : 13 : This program is distributed in the hope that it will be useful, 14 : but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : GNU General Public License for more details. 17 : 18 : You should have received a copy of the GNU General Public License 19 : along with this program; if not, write to the Free Software 20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 21 : MA 02110-1301, USA. 22 : 23 : */ 24 : 25 : #import <Foundation/Foundation.h> 26 : #import "NSMutableDictionaryOOExtensions.h" 27 : 28 : @implementation NSMutableDictionary (OOExtensions) 29 : 30 : - (void)mergeEntriesFromDictionary:(NSDictionary *)otherDictionary 31 : { 32 : NSEnumerator *otherKeysEnum = nil; 33 : id key = nil; 34 : 35 : for (otherKeysEnum = [otherDictionary keyEnumerator]; (key = [otherKeysEnum nextObject]); ) 36 : { 37 : if (![self objectForKey:key]) 38 : [self setObject:[otherDictionary objectForKey:key] forKey:key]; 39 : else 40 : { 41 : BOOL merged = NO; 42 : id thisObject = [self objectForKey:key]; 43 : id otherObject = [otherDictionary objectForKey:key]; 44 : 45 : if ([thisObject isKindOfClass:[NSDictionary class]]&&[otherObject isKindOfClass:[NSDictionary class]]&&(![thisObject isEqual:otherObject])) 46 : { 47 : NSMutableDictionary* mergeObject = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary*)thisObject]; 48 : [mergeObject mergeEntriesFromDictionary:(NSDictionary*)otherObject]; 49 : [self setObject:mergeObject forKey:key]; 50 : merged = YES; 51 : } 52 : 53 : if ([thisObject isKindOfClass:[NSArray class]]&&[otherObject isKindOfClass:[NSArray class]]&&(![thisObject isEqual:otherObject])) 54 : { 55 : NSMutableArray* mergeObject = [NSMutableArray arrayWithArray:(NSArray*)thisObject]; 56 : [mergeObject addObjectsFromArray:(NSArray*)otherObject]; 57 : [self setObject:mergeObject forKey:key]; 58 : merged = YES; 59 : } 60 : 61 : if (!merged) 62 : [self setObject:otherObject forKey:key]; 63 : } 64 : } 65 : } 66 : 67 : @end