Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
NSDictionaryOOExtensions.m
Go to the documentation of this file.
1/*
2
3NSDictionaryOOExtensions.m
4
5
6Copyright (C) 2008-2013 Jens Ayton and contributors
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
29
30
31@implementation NSDictionary (OOExtensions)
32
33- (NSDictionary *) dictionaryByAddingObject:(id)object forKey:(id)key
34{
35 // Note: object lifetime issues aside, we need to copy and autorelease so that the right thing happens for mutable dictionaries.
36 if (object == nil || key == nil) return [[self copy] autorelease];
37
38 NSMutableDictionary *temp = [self mutableCopy];
39 [temp setObject:object forKey:key];
40 NSDictionary *result = [[temp copy] autorelease];
41 [temp release];
42
43 return result;
44}
45
46
47- (NSDictionary *) dictionaryByRemovingObjectForKey:(id)key
48{
49 // Note: object lifetime issues aside, we need to copy and autorelease so that the right thing happens for mutable dictionaries.
50 if (key == nil) return [[self copy] autorelease];
51
52 NSMutableDictionary *temp = [self mutableCopy];
53 [temp removeObjectForKey:key];
54 NSDictionary *result = [[temp copy] autorelease];
55 [temp release];
56
57 return result;
58}
59
60
61- (NSDictionary *) dictionaryByAddingEntriesFromDictionary:(NSDictionary *)dictionary
62{
63 // Note: object lifetime issues aside, we need to copy and autorelease so that the right thing happens for mutable dictionaries.
64 if (dictionary == nil) return [[self copy] autorelease];
65
66 NSMutableDictionary *temp = [self mutableCopy];
67 [temp addEntriesFromDictionary:dictionary];
68 NSDictionary *result = [[temp copy] autorelease];
69 [temp release];
70
71 return result;
72}
73
74@end
return nil