NSDictionary+Safety.m 1.2 KB
//
//  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