Adds JSONHelper methods to read numbers as ints.

This commit is contained in:
2020-09-17 00:30:22 -07:00
parent 82625d4548
commit 4a1145fd28
3 changed files with 122 additions and 0 deletions

View File

@@ -16,10 +16,15 @@ NS_ASSUME_NONNULL_BEGIN
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSString* _Nullable)defaultValue;
+(NSNumber*)readNumberFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
+(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;
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index;
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue;
+(NSNumber*)readNumberFromArray:(NSArray*)array forIndex:(NSUInteger)index;
+(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;
@end

View File

@@ -26,6 +26,23 @@ NSNumber* coerceObjectToNumber(NSObject *object, NSNumber *defaultValue) {
return defaultValue;
}
int coerceObjectToInt(NSObject *object, int defaultValue) {
if ([object isKindOfClass:[NSNumber class]]) {
return [(NSNumber*)object intValue];
}
if ([object isKindOfClass:[NSString class]]) {
NSScanner *scanner;
int temp;
scanner = [NSScanner scannerWithString:(NSString*)object];
if ([scanner scanInt:&temp]) {
return temp;
}
}
return defaultValue;
}
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
return [JSONHelper readStringFromDictionary:dictionary forKey:key withDefaultValue:nil];
}
@@ -44,6 +61,15 @@ NSNumber* coerceObjectToNumber(NSObject *object, NSNumber *defaultValue) {
return coerceObjectToNumber(object, defaultValue);
}
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
return [JSONHelper readIntFromDictionary:dictionary forKey:key withDefaultValue:0];
}
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(int)defaultValue {
NSObject *object = [dictionary objectForKey:key];
return coerceObjectToInt(object, defaultValue);
}
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{
return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil];
}
@@ -62,4 +88,13 @@ NSNumber* coerceObjectToNumber(NSObject *object, NSNumber *defaultValue) {
return coerceObjectToNumber(object, defaultValue);
}
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index {
return [JSONHelper readIntFromArray:array forIndex:index withDefaultValue:0];
}
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(int)defaultValue {
NSObject *object = [array objectAtIndex:index];
return coerceObjectToInt(object, defaultValue);
}
@end