From 156f70bb9741b234e623e0fdee70b39db2fed170 Mon Sep 17 00:00:00 2001 From: pengfeizhou Date: Mon, 1 Feb 2021 17:12:56 +0800 Subject: [PATCH] iOS:Add safe opt methods for NSDictionary,check type error --- .../Classes/Extension/DoricBridgeExtension.m | 6 +- doric-iOS/Pod/Classes/Util/DoricExtensions.h | 22 ++++++ doric-iOS/Pod/Classes/Util/DoricExtensions.m | 78 +++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m b/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m index e8c1ab88..86369d01 100644 --- a/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m +++ b/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m @@ -33,7 +33,7 @@ @implementation DoricBridgeExtension - (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) { 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 { __strong DoricContext *strongContext = context; - + unsigned int count; id ret = nil; Method *methods = class_copyMethodList(clz, &count); @@ -109,7 +109,7 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met } else { DoricLog(@"CallNative Error:%@", @"Must return object type"); [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; } } diff --git a/doric-iOS/Pod/Classes/Util/DoricExtensions.h b/doric-iOS/Pod/Classes/Util/DoricExtensions.h index ce3998a8..2f418966 100644 --- a/doric-iOS/Pod/Classes/Util/DoricExtensions.h +++ b/doric-iOS/Pod/Classes/Util/DoricExtensions.h @@ -43,3 +43,25 @@ - (NSArray *)filter:(BOOL (NS_NOESCAPE ^)(ObjectType obj))block; @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 \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Util/DoricExtensions.m b/doric-iOS/Pod/Classes/Util/DoricExtensions.m index 2feae596..b14cdefb 100644 --- a/doric-iOS/Pod/Classes/Util/DoricExtensions.m +++ b/doric-iOS/Pod/Classes/Util/DoricExtensions.m @@ -18,6 +18,7 @@ // #import "DoricExtensions.h" +#import "DoricUtil.h" @implementation NSObject (Doric) - (instancetype)also:(void (NS_NOESCAPE ^)(id it))block { @@ -96,3 +97,80 @@ - (NSArray *)filter:(BOOL (NS_NOESCAPE ^)(id obj))block { } @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 \ No newline at end of file