NSDictionary+YOHO.m
4.04 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// NSDictionary+YOHO.m
// YH_Marketplace
//
// Created by Liu Xiangchao on 7/15/14.
// Copyright (c) 2014 YOHO. All rights reserved.
//
#import "NSDictionary+YOHO.h"
@implementation NSDictionary(YOHO)
- (id)yh_object:(id)options key:(NSString *)key
{
NSArray *keys = [key componentsSeparatedByString:@"."];
for (NSString *_key in keys) {
if ([options isKindOfClass:[NSArray class]]) {
int _intKey = [_key intValue];
if ([(NSArray *)options count] <= _intKey) {
return nil;
}
options = [(NSArray *) options objectAtIndex:_intKey];
}else if ([options isKindOfClass:[NSDictionary class]]) {
options = [(NSDictionary *)options objectForKey:_key];
if (options != nil && [options isKindOfClass:[NSNull class]]) {
options = nil;
}
}
}
return options;
}
- (id)yh_objectForPath:(NSString *)path
{
return [self yh_object:self key:path];
}
- (NSString *)yh_stringForPath:(NSString *) path
{
id o = [self yh_objectForPath:path];
if (o == nil) {
return @"";
}
if ([o isKindOfClass:[NSNull class]]) {
return @"";
}
NSString *s = [NSString stringWithFormat:@"%@", o];
if ([s isEqualToString:@"<null>"]) {
s = @"";
}
return s;
}
- (NSDictionary *)yh_dictForKey:(id)key
{
id object = [self objectForKey:key];
if ((object == nil)||[object isEqual:[NSNull null]]) {
object = @{};
}
if (![object isKindOfClass:[NSDictionary class]]) {
object = @{};
}
return object;
}
- (NSArray *)yh_arrayForKey:(id)key
{
id object = [self objectForKey:key];
if ((object == nil)||[object isEqual:[NSNull null]]) {
object = @[];
}
if (![object isKindOfClass:[NSArray class]]) {
object = @[];
}
return object;
}
- (NSString *)yh_stringForKey:(id)key
{
id object = [self objectForKey:key];
if ((object == nil) ||[object isEqual:[NSNull null]]) {
return @"";
}
if (![object isKindOfClass:[NSString class]]) {
return [NSString stringWithFormat:@"%@", object];
}
return object;
}
- (BOOL)yh_boolForKey:(NSString *)key
{
id object = self[key];
if ([object isEqual:[NSNull null]]) {
return NO;
}
if ([object isKindOfClass:[NSNumber class]]) {
return [object boolValue];
}
if ([object isKindOfClass:[NSString class]]) {
return [object boolValue];
}
return NO;
}
- (NSInteger)yh_integerForKey:(NSString *)key
{
NSString *string = [self yh_objectForPath:key];
if ([string respondsToSelector:@selector(integerValue)]) {
return [string integerValue];
}
return 0;
}
- (long)yh_longForKey:(NSString *)key
{
NSNumber *number = [self yh_objectForPath:key];
return [number longValue];
}
- (long long)yh_longlongForKey:(NSString *)key
{
NSNumber *number = [self yh_objectForPath:key];
return [number longLongValue];
}
- (float)yh_floatForKey:(NSString *)key
{
NSString *string = [self yh_objectForPath:key];
return [string floatValue];
}
- (double)yh_doubleForKey:(NSString *)key
{
NSString *string = [self yh_objectForPath:key];
return [string doubleValue];
}
- (NSMutableDictionary *)yh_mutableDeepCopy
{
NSMutableDictionary *ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
NSArray *keys = [self allKeys];
for (id key in keys){
id oneValue = [self valueForKey:key];
id oneCopy = nil;
if ([oneValue respondsToSelector:@selector(yh_mutableDeepCopy)]){
oneCopy = [oneValue yh_mutableDeepCopy];
}else if ([oneValue respondsToSelector:@selector(mutableCopy)]){
oneCopy = [oneValue mutableCopy];
}
if (oneCopy == nil){
oneCopy = [oneValue copy];
}
[ret setValue:oneCopy forKey:key];
}
return ret;
}
- (NSString *)yh_asJSONString
{
NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
@end