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