iOS开发NSDictionary+Addition

释放双眼,带上耳机,听听看~!

对从后台取得数据进行安全处理

.h文件

@interface NSDictionary (SafeAccess)
- (NSString*)stringForKey:(id)key;
- (NSNumber*)numberForKey:(id)key;
- (NSArray*)arrayForKey:(id)key;
- (NSDictionary*)dictionaryForKey:(id)key;
- (NSInteger)integerForKey:(id)key;
- (BOOL)boolForKey:(id)key;
- (float)floatForKey:(id)key;
- (double)doubleForKey:(id)key;
//CG
- (CGFloat)CGFloatForKey:(id)key;
- (CGPoint)pointForKey:(id)key;
- (CGSize)sizeForKey:(id)key;
- (CGRect)rectForKey:(id)key;
@end
#pragma --mark NSMutableDictionary setter
@interface NSMutableDictionary(SafeAccess)
-(void)setObj:(id)i forKey:(NSString*)key;
-(void)setString:(NSString*)i forKey:(NSString*)key;
-(void)setBool:(BOOL)i forKey:(NSString*)key;
-(void)setInt:(int)i forKey:(NSString*)key;
-(void)setInteger:(NSInteger)i forKey:(NSString*)key;
-(void)setCGFloat:(CGFloat)f forKey:(NSString*)key;
-(void)setFloat:(float)i forKey:(NSString*)key;
-(void)setPoint:(CGPoint)o forKey:(NSString*)key;
-(void)setSize:(CGSize)o forKey:(NSString*)key;
-(void)setRect:(CGRect)o forKey:(NSString*)key;
@end

.m文件

@implementation NSDictionary (SafeAccess)
- (NSString *)stringForKey:(id)key
{
id value = [self objectForKey:key];
if (value == nil || value == [NSNull null])
{
return @"";
}
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;
}
- (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;
}
- (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;
}
//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)setCGFloat:(CGFloat)f forKey:(NSString *)key
{
self[key] = @(f);
}
-(void)setFloat:(float)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

人已赞赏
iOS文章

iOSTweak底层实现Cydia Substrate

2020-3-4 15:09:00

iOS文章

iOS标签滚动字幕

2020-3-4 17:38:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索