Line data Source code
1 0 : /* 2 : 3 : NSDictionaryOOExtensions.m 4 : 5 : 6 : Copyright (C) 2008-2013 Jens Ayton and contributors 7 : 8 : Permission is hereby granted, free of charge, to any person obtaining a copy 9 : of this software and associated documentation files (the "Software"), to deal 10 : in the Software without restriction, including without limitation the rights 11 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 : copies of the Software, and to permit persons to whom the Software is 13 : furnished to do so, subject to the following conditions: 14 : 15 : The above copyright notice and this permission notice shall be included in all 16 : copies or substantial portions of the Software. 17 : 18 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 : SOFTWARE. 25 : 26 : */ 27 : 28 : #import "NSDictionaryOOExtensions.h" 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