123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- //
- // 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
|