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

@ -33,7 +33,7 @@
@implementation DoricBridgeExtension @implementation DoricBridgeExtension
- (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module method:(NSString *)method callbackId:(NSString *)callbackId argument:(id)argument { - (id)callNativeWithContextId:(NSString *)contextId module:(NSString *)module method:(NSString *)method callbackId:(NSString *)callbackId argument:(id)argument {
__strong DoricContext *context = [[DoricContextManager instance] getContext:contextId]; __strong DoricContext *context = [[DoricContextManager instance] getContext:contextId];
if (context.destroyed) { if (context.destroyed) {
return nil; return nil;
} }
@ -56,7 +56,7 @@ - (id)createParamWithMethodName:(NSString *)method context:(DoricContext *)conte
- (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context method:(NSString *)name callbackId:(NSString *)callbackId argument:(id)argument { - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context method:(NSString *)name callbackId:(NSString *)callbackId argument:(id)argument {
__strong DoricContext *strongContext = context; __strong DoricContext *strongContext = context;
unsigned int count; unsigned int count;
id ret = nil; id ret = nil;
Method *methods = class_copyMethodList(clz, &count); Method *methods = class_copyMethodList(clz, &count);
@ -109,7 +109,7 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met
} else { } else {
DoricLog(@"CallNative Error:%@", @"Must return object type"); DoricLog(@"CallNative Error:%@", @"Must return object type");
[strongContext.driver.registry onLog:DoricLogTypeError [strongContext.driver.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"CallNative Error:%@", @"Must return object type"]]; message:[NSString stringWithFormat:@"CallNative Error:%@", @"Must return object type"]];
ret = nil; ret = nil;
} }
} }

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