iOS:Add safe opt methods for NSDictionary,check type error

This commit is contained in:
pengfeizhou 2021-02-01 17:12:56 +08:00 committed by osborn
parent 5a12a770fd
commit 156f70bb97
3 changed files with 103 additions and 3 deletions

View File

@ -43,3 +43,25 @@
- (NSArray <ObjectType> *)filter:(BOOL (NS_NOESCAPE ^)(ObjectType obj))block; - (NSArray <ObjectType> *)filter:(BOOL (NS_NOESCAPE ^)(ObjectType obj))block;
@end @end
@interface NSDictionary (Doric)
- (NSString *)optString:(NSString *)key;
- (NSNumber *)optNumber:(NSString *)key;
- (NSDictionary *)optObject:(NSString *)key;
- (NSArray *)optArray:(NSString *)key;
- (bool)optBool:(NSString *)key;
- (NSString *)optString:(NSString *)key defaultValue:(NSString *)value;
- (NSNumber *)optNumber:(NSString *)key defaultValue:(NSNumber *)value;
- (NSDictionary *)optObject:(NSString *)key defaultValue:(NSDictionary *)value;
- (NSArray *)optArray:(NSString *)key defaultValue:(NSArray *)value;
- (bool)optBool:(NSString *)key defaultValue:(bool)value;
@end

View File

@ -18,6 +18,7 @@
// //
#import "DoricExtensions.h" #import "DoricExtensions.h"
#import "DoricUtil.h"
@implementation NSObject (Doric) @implementation NSObject (Doric)
- (instancetype)also:(void (NS_NOESCAPE ^)(id it))block { - (instancetype)also:(void (NS_NOESCAPE ^)(id it))block {
@ -96,3 +97,80 @@ - (NSArray *)filter:(BOOL (NS_NOESCAPE ^)(id obj))block {
} }
@end @end
@implementation NSDictionary (Doric)
- (NSString *)optString:(NSString *)key {
return [self optString:key defaultValue:nil];
}
- (NSNumber *)optNumber:(NSString *)key {
return [self optNumber:key defaultValue:nil];
}
- (NSDictionary *)optObject:(NSString *)key {
return [self optObject:key defaultValue:nil];
}
- (NSArray *)optArray:(NSString *)key {
return [self optArray:key defaultValue:nil];
}
- (bool)optBool:(NSString *)key {
return [self optBool:key defaultValue:NO];
}
- (NSString *)optString:(NSString *)key defaultValue:(NSString *)value {
id val = self[key];
if ([val isKindOfClass:NSString.class]) {
return val;
} else if (val) {
DoricLog(@"Doric Type Error: %@, key = %@, value = %@", self, key, val);
}
return value;
}
- (NSNumber *)optNumber:(NSString *)key defaultValue:(NSNumber *)value {
id val = self[key];
if ([val isKindOfClass:NSNumber.class]) {
return val;
} else if (val) {
DoricLog(@"Doric Type Error: %@, key = %@, value = %@", self, key, val);
}
return value;
}
- (NSDictionary *)optObject:(NSString *)key defaultValue:(NSDictionary *)value {
id val = self[key];
if ([val isKindOfClass:NSDictionary.class]) {
return val;
} else if (val) {
DoricLog(@"Doric Type Error: %@, key = %@, value = %@", self, key, val);
}
return value;
}
- (NSArray *)optArray:(NSString *)key defaultValue:(NSArray *)value {
id val = self[key];
if ([val isKindOfClass:NSArray.class]) {
return val;
} else if (val) {
DoricLog(@"Doric Type Error: %@, key = %@, value = %@", self, key, val);
}
return value;
}
- (bool)optBool:(NSString *)key defaultValue:(bool)value {
id val = self[key];
if ([val isKindOfClass:NSNumber.class]) {
return ((NSNumber *) val).boolValue;
} else if (val) {
DoricLog(@"Doric Type Error: %@, key = %@, value = %@", self, key, val);
}
return value;
}
@end