-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSDictionary+Convenience.m
139 lines (105 loc) · 3.08 KB
/
NSDictionary+Convenience.m
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// NSDictionary+Convenience.m
// Sleep Diary
//
// Created by Alexander Ivanov on 19.11.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "NSDictionary+Convenience.h"
@implementation NSDictionary (Convenience)
- (id)objectForNullableKey:(id<NSCopying>)aKey {
return aKey ? [self objectForKey:aKey] : Nil;
}
- (NSDictionary *)dictionaryWithObjectsForKeys:(NSArray *)keys {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:keys.count];
for (id key in keys)
dictionary[key] = self[key];
return dictionary;
}
- (instancetype)dictionaryWithObject:(id)object forKey:(id<NSCopying>)key {
NSMutableDictionary *dictionary = [self mutableCopy];
if (object)
dictionary[key] = object;
else
[dictionary removeObjectForKey:key];
return dictionary;
}
- (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
NSMutableDictionary *dictionary = [self mutableCopy];
for (NSUInteger index = 0; index < keys.count; index++) {
id object = index < objects.count ? objects[index] : Nil;
id<NSCopying> key = keys[index];
if (object)
dictionary[key] = object;
else
[dictionary removeObjectForKey:key];
}
return dictionary;
}
- (instancetype)dictionaryWithEntriesFromDictionary:(NSDictionary *)otherDictionary {
NSMutableDictionary *dictionary = [self mutableCopy];
if (otherDictionary)
[dictionary addEntriesFromDictionary:otherDictionary];
return dictionary;
}
- (NSDictionary *)mapKeys:(id (^)(id))keyBlock values:(id (^)(id))valBlock {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for (id key in self) {
id val = self[key];
id k = keyBlock ? keyBlock(key) : key;
id v = valBlock ? valBlock(val) : val;
if (k && v)
dictionary[k] = v;
}
return dictionary;
}
- (NSDictionary *)mapKeys:(id (^)(id))block {
return [self mapKeys:block values:Nil];
}
- (NSDictionary *)mapValues:(id (^)(id))block {
return [self mapKeys:Nil values:block];
}
- (NSArray *)array:(NSArray *(^)(id, id))block {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:2 * self.count];
for (id key in self) {
id val = self[key];
NSArray *objects = block ? block(key, val) : @[ key, val ];
if (objects)
[array addObjectsFromArray:objects];
}
return array;
}
- (NSArray *)array {
return [self array:Nil];
}
- (BOOL)boolForKey:(NSString *)key {
return [self[key] boolValue];
}
- (double)doubleForKey:(NSString *)key {
return [self[key] doubleValue];
}
- (NSInteger)integerForKey:(NSString *)key {
return [self[key] integerValue];
}
- (NSUInteger)unsignedIntegerForKey:(NSString *)key {
return [self[key] integerValue];
}
@end
@implementation NSMutableDictionary (Convenience)
- (void)setBool:(BOOL)value forKey:(NSString *)key {
if (value)
self[key] = @(value);
}
- (void)setDouble:(double)value forKey:(NSString *)key {
if (value != 0.0)
self[key] = @(value);
}
- (void)setInteger:(NSInteger)value forKey:(NSString *)key {
if (value != 0)
self[key] = @(value);
}
- (void)setUnsignedInteger:(NSUInteger)value forKey:(NSString *)key {
if (value != 0)
self[key] = @(value);
}
@end