-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSMutableDictionary+QuickValueModification.m
More file actions
48 lines (38 loc) · 1.21 KB
/
NSMutableDictionary+QuickValueModification.m
File metadata and controls
48 lines (38 loc) · 1.21 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
//
// NSMutableDictionary+SImpleMath.m
//
// Created by Jack Wu on 2014-05-23.
//
#import "NSMutableDictionary+QuickValueModification.h"
@implementation NSMutableDictionary (QuickValueModification)
- (void)incrementIntValueOfKey:(NSString *)key {
[self modifyIntegerValueOfKey:key withBlock:^NSInteger(NSInteger i){
return ++i;
}];
}
- (void)decrementIntValueOfKey:(NSString *)key {
[self modifyIntegerValueOfKey:key withBlock:^NSInteger(NSInteger i){
return --i;
}];
}
- (void)modifyIntegerValueOfKey:(NSString*)key withBlock:(NSInteger (^)(NSInteger i))operation {
if (!self[key]) {
self[key] = @(0);
}
else if (![self[key] isKindOfClass:[NSNumber class]]) {
NSLog(@"Value to modify is incorrect type");
return;
}
self[key] = @(operation([self[key] integerValue]));
}
- (void)modifyFloatValueOfKey:(NSString*)key withBlock:(CGFloat (^)(CGFloat f))operation {
if (!self[key]) {
self[key] = @(0.0f);
}
if (![self[key] isKindOfClass:[NSNumber class]]) {
NSLog(@"Value to modify is incorrect type");
return;
}
self[key] = @(operation([self[key] floatValue]));
}
@end