-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSDictionary+TypedAccessors.m
More file actions
64 lines (48 loc) · 1.36 KB
/
NSDictionary+TypedAccessors.m
File metadata and controls
64 lines (48 loc) · 1.36 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
#import "NSDictionary+TypedAccessors.h"
@implementation NSDictionary (TypedAccessors)
- (NSString*)stringForKey:(NSString*)key
{
NSObject* value = [self objectForKey:key];
if ([value isKindOfClass:[NSString class]]) {
return (NSString*)value;
}
if ([value isKindOfClass:[NSNumber class]]) {
return [NSString stringWithFormat:@"%@", value];
}
return nil;
}
- (NSNumber*)numberForKey:(NSString*)key
{
NSObject* value = [self objectForKey:key];
if ([value isKindOfClass:[NSNumber class]]) {
return (NSNumber*)value;
}
if ([value isKindOfClass:[NSString class]]) {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
return [f numberFromString:(NSString*)value];
}
return nil;
}
- (NSArray*)arrayForKey:(NSString*)key
{
NSObject* value = [self objectForKey:key];
if (!value) {
return nil;
}
if ([value isKindOfClass:[NSArray class]]) {
return (NSArray*)value;
}
else {
return [NSArray arrayWithObject:value];
}
}
- (NSDictionary*)dictionaryForKey:(NSString*)key
{
NSDictionary* subDictionary = [self objectForKey:key];
if ([subDictionary isKindOfClass:[NSDictionary class]]) {
return subDictionary;
}
return nil;
}
@end