// // NSDictionary+SafeAccess.m // IOS-Categories // // Created by Jakey on 15/1/25. // Copyright (c) 2015年 www.skyfox.org. All rights reserved. // #import "NSDictionary+SafeAccess.h" @implementation NSDictionary (SafeAccess) - (BOOL)hasKey:(NSString *)key { return [self objectForKey:key] != nil; } - (NSString*)stringForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return nil; } if ([value isKindOfClass:[NSString class]]) { return (NSString*)value; } if ([value isKindOfClass:[NSNumber class]]) { return [value stringValue]; } return nil; } - (NSNumber*)numberForKey:(id)key { id 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:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return nil; } if ([value isKindOfClass:[NSArray class]]) { return value; } return nil; } - (NSDictionary*)dictionaryForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return nil; } if ([value isKindOfClass:[NSDictionary class]]) { return value; } return nil; } - (NSInteger)integerForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) { return [value integerValue]; } return 0; } - (NSUInteger)unsignedIntegerForKey:(id)key{ id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) { return [value unsignedIntegerValue]; } return 0; } - (BOOL)boolForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return NO; } if ([value isKindOfClass:[NSNumber class]]) { return [value boolValue]; } if ([value isKindOfClass:[NSString class]]) { return [value boolValue]; } return NO; } - (int16_t)int16ForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]]) { return [value shortValue]; } if ([value isKindOfClass:[NSString class]]) { return [value intValue]; } return 0; } - (int32_t)int32ForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) { return [value intValue]; } return 0; } - (int64_t)int64ForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) { return [value longLongValue]; } return 0; } - (char)charForKey:(id)key{ id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) { return [value charValue]; } return 0; } - (short)shortForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]]) { return [value shortValue]; } if ([value isKindOfClass:[NSString class]]) { return [value intValue]; } return 0; } - (float)floatForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) { return [value floatValue]; } return 0; } - (double)doubleForKey:(id)key { id value = [self objectForKey:key]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) { return [value doubleValue]; } return 0; } - (long long)longLongForKey:(id)key { id value = [self objectForKey:key]; if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) { return [value longLongValue]; } return 0; } - (unsigned long long)unsignedLongLongForKey:(id)key { id value = [self objectForKey:key]; if ([value isKindOfClass:[NSString class]]) { NSNumberFormatter *nf = [[NSNumberFormatter alloc] init]; value = [nf numberFromString:value]; } if ([value isKindOfClass:[NSNumber class]]) { return [value unsignedLongLongValue]; } return 0; } //CG - (CGFloat)CGFloatForKey:(id)key { CGFloat f = [self[key] doubleValue]; return f; } - (CGPoint)pointForKey:(id)key { CGPoint point = CGPointFromString(self[key]); return point; } - (CGSize)sizeForKey:(id)key { CGSize size = CGSizeFromString(self[key]); return size; } - (CGRect)rectForKey:(id)key { CGRect rect = CGRectFromString(self[key]); return rect; } @end #pragma --mark NSMutableDictionary setter @implementation NSMutableDictionary (SafeAccess) -(void)setObj:(id)i forKey:(NSString*)key{ if (i!=nil) { self[key] = i; } } -(void)setString:(NSString*)i forKey:(NSString*)key; { [self setValue:i forKey:key]; } -(void)setBool:(BOOL)i forKey:(NSString *)key { self[key] = @(i); } -(void)setInt:(int)i forKey:(NSString *)key { self[key] = @(i); } -(void)setInteger:(NSInteger)i forKey:(NSString *)key { self[key] = @(i); } -(void)setUnsignedInteger:(NSUInteger)i forKey:(NSString *)key { self[key] = @(i); } -(void)setCGFloat:(CGFloat)f forKey:(NSString *)key { self[key] = @(f); } -(void)setChar:(char)c forKey:(NSString *)key { self[key] = @(c); } -(void)setFloat:(float)i forKey:(NSString *)key { self[key] = @(i); } -(void)setDouble:(double)i forKey:(NSString*)key{ self[key] = @(i); } -(void)setLongLong:(long long)i forKey:(NSString*)key{ self[key] = @(i); } -(void)setPoint:(CGPoint)o forKey:(NSString *)key { self[key] = NSStringFromCGPoint(o); } -(void)setSize:(CGSize)o forKey:(NSString *)key { self[key] = NSStringFromCGSize(o); } -(void)setRect:(CGRect)o forKey:(NSString *)key { self[key] = NSStringFromCGRect(o); } @end