-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNSDictionary+Coby.m
More file actions
113 lines (101 loc) · 3.5 KB
/
NSDictionary+Coby.m
File metadata and controls
113 lines (101 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#import <Foundation/Foundation.h>
#import "NSDictionary+Coby.h"
@implementation NSDictionary (Coby)
// Calls block once for each key in the dictionary, passing the
// key-value pair as parameters.
- (void)each: (void (^)(id key, id value))block {
[self enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
block(key, obj);
}];
}
// Returns a new dictionary containing the contents of
// `otherDictionary` and the contents of itself.
//
// The value for each duplicate key will be the value of the key
// found in `otherDictionary`.
- (NSDictionary *)merge:(NSDictionary *)otherDictionary {
NSMutableDictionary * result = [NSMutableDictionary dictionaryWithDictionary:self];
[otherDictionary each: ^(id key, id obj) {
[result setObject:obj forKey:key];
}];
#if __has_feature(objc_arc)
return (NSDictionary *) [result mutableCopy];
#else
return (NSDictionary *) [[result mutableCopy] autorelease];
#endif
}
// The value for each duplicate key is determined by calling the
// block with the key, its value in hsh and its value in other_hash.
//
// dict1 = [NSDictionary dictionaryWithObjectsAndKeys:
// @"a value", @"a",
// @"b value", @"b",nil];
// dict2 = [NSDictionary dictionaryWithObjectsAndKeys:
// @"c value", @"c",
// @"newer value", @"b",nil];
//
// [dict1 merge:dict2 withBlock:^(id key, id old, id new){
// return [NSString stringWithFormat:@"%@ - %@",
// oldValue, newValue];
// }];
//
// => {"a" => @"a value", "b" => @"b value - newer value", "c" => @"c value"}
- (NSDictionary *)merge:(NSDictionary *)otherDictionary withBlock:(id (^)(NSString *key, id oldValue, id newValue))block {
NSMutableDictionary * result = [NSMutableDictionary dictionaryWithDictionary:self];
[otherDictionary each: ^(id key, id obj) {
if ([self fetch:key]) {
id object = block(key, [self fetch:key], obj);
[result setObject:object forKey:key];
} else {
[result setObject:obj forKey:key];
}
}];
#if __has_feature(objc_arc)
return (NSDictionary *) [result mutableCopy];
#else
return (NSDictionary *) [[result mutableCopy] autorelease];
#endif
}
// # Fetch
// Returns a value from the `NSDictionary` for the given key.
//
//
- (id)fetch:(NSString *)key {
return [self objectForKey:key];
}
//
// If a default is given, then that will be returned for a missing
// key. This is especially useful when assigning Dict-values to an
// array. Since we can make sure something other than `nil` is
// returned.
//
// [dict fetch:@"unknown" default:@""];
//
- (id)fetch:(NSString *)key default:(id)defaultValue {
if(![self fetch:key]) return defaultValue;
return [self fetch:key];
}
// If a code block is specified, then that will be run and its result
// returned.
//
// [dict fetch:@"unknownKey" withBlock:^id(id keyOrValue) {
// return [NSString stringWithFormat:@"Looked for: %@", obj];
// }];
// => @"Looked for: unknownKey"
//
// [dict fetch:@"knownKey" withBlock:^id(id keyOrValue) {
// return [NSString stringWithFormat:@"Looked for: %@", obj];
// }];
// => @"Looked for: value_for_knownKey"
//
- (id)fetch:(NSString *)key withBlock:(id (^)(id keyOrValue))block {
if(![self fetch:key]) return block(key);
return block([self fetch:key]);
}
@end
@implementation NSMutableDictionary (Coby)
- (void)set:(id)object for:(NSString *)key {
if (!object) return;
[self setObject:object forKey:key];
}
@end