NSDictionary+Safety.m
1.2 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
//
// NSDictionary+Safety.m
// CNLiveUserSystemDemo
//
// Created by iOS on 16/9/21.
// Copyright © 2016年 zhulin. All rights reserved.
//
#import "NSDictionary+Safety.h"
@implementation NSDictionary (Safety)
- (NSDictionary *)validatedDictionary {
if (!self || ![self isKindOfClass:[NSDictionary class]]) {
return nil;
}
NSDictionary *temp = (NSDictionary *)self;
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if (temp) {
id keys = [temp allKeys];
for (id key in keys) {
if (temp[key] || ![[temp objectForKey:key] isKindOfClass:[NSNull class]]) {
if ([temp[key] isKindOfClass:[NSNumber class]]) {
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
NSString *str = [numberFormatter stringFromNumber:temp[key]];
[dic setObject:str forKey:key];
}
else {
[dic setObject:temp[key] forKey:key];
}
} else {
[dic setObject:@"" forKey:key];
}
}
}
return dic;
}
@end