Oolite 1.91.0.7646-241128-10e222e
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
25#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 {
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
return nil