iOS:Add safe opt methods for NSDictionary,check type error
This commit is contained in:
parent
5a12a770fd
commit
156f70bb97
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -43,3 +43,25 @@
|
||||
|
||||
- (NSArray <ObjectType> *)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
|
@ -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
|
Reference in New Issue
Block a user