Adds methods to JSONHelper to read boolean values.

This commit is contained in:
2020-09-17 00:32:53 -07:00
parent 4a1145fd28
commit 9bf1595f29
3 changed files with 122 additions and 0 deletions

View File

@@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
+(NSNumber*)readNumberFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSNumber* _Nullable)defaultValue;
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(int)defaultValue;
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(BOOL)defaultValue;
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index;
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue;
@@ -25,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
+(NSNumber*)readNumberFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSNumber* _Nullable)defaultValue;
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index;
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(int)defaultValue;
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index;
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(BOOL)defaultValue;
@end

View File

@@ -43,6 +43,14 @@ int coerceObjectToInt(NSObject *object, int defaultValue) {
return defaultValue;
}
BOOL coerceObjectToBool(NSObject *object, BOOL defaultValue) {
if ([object isKindOfClass:[NSNumber class]]) {
return [(NSNumber*)object boolValue];
}
return defaultValue;
}
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
return [JSONHelper readStringFromDictionary:dictionary forKey:key withDefaultValue:nil];
}
@@ -70,6 +78,15 @@ int coerceObjectToInt(NSObject *object, int defaultValue) {
return coerceObjectToInt(object, defaultValue);
}
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
return [JSONHelper readBoolFromDictionary:dictionary forKey:key withDefaultValue:NO];
}
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(BOOL)defaultValue {
NSObject *object = [dictionary objectForKey:key];
return coerceObjectToBool(object, defaultValue);
}
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{
return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil];
}
@@ -97,4 +114,13 @@ int coerceObjectToInt(NSObject *object, int defaultValue) {
return coerceObjectToInt(object, defaultValue);
}
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index {
return [JSONHelper readBoolFromArray:array forIndex:index withDefaultValue:nil];
}
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(BOOL)defaultValue {
NSObject *object = [array objectAtIndex:index];
return coerceObjectToBool(object, defaultValue);
}
@end