//
//  SafeForKey.m
//  SecondBrush
//
//  Created by zzs on 2015/12/14.
//  Copyright © 2016年 allen. All rights reserved.
//

#import "SafeForKey.h"

typedef NS_ENUM(NSInteger, safeForKeyType) {
    safeString,
    safeStringInt,
    safeArray,
    safeDictionary
};



@implementation NSDictionary (safeforkey)


-(id)safeStringForKey:(NSString *)key{
    return [self safeObjectForKey:key withSafeForKeyStype:safeString];
}

-(id)safeStringIntForKey:(NSString *)key{
  return [self safeObjectForKey:key withSafeForKeyStype:safeStringInt];
}

-(id)safeArrayForKey:(NSString *)key{
    return [self safeObjectForKey:key withSafeForKeyStype:safeArray];
}

-(id)safeDictionaryForKey:(NSString *)key{
    return [self safeObjectForKey:key withSafeForKeyStype:safeDictionary];
}

-(BOOL)isNUll:(id)object
{
    if ([object isEqual:[NSNull null]] || object == nil || object == NULL) {
        return YES;
    }
    return NO;
}

-(id)safeObjectForKey:(NSString *)key withSafeForKeyStype:(safeForKeyType) type
{
    id  object = [self objectForKey:key];
    
    if (![self isNUll:object]) {
        return object;
    }
    
    switch (type) {
        case safeString:
        {
           return @"";
        }
            break;
        case safeStringInt:
        {
            return @"0";
        }
            break;
        case safeArray:
        {
            return [NSArray array];
        }
            break;
        case safeDictionary:
        {
            return [NSDictionary dictionary];
        }
            break;

    }
}

@end

