From ba2fbfaa60dea30a99509e998fe271d202b3a4e3 Mon Sep 17 00:00:00 2001 From: Andrea Busi Date: Sat, 3 Oct 2020 12:01:17 +0200 Subject: [PATCH] feat: Handle weather code as nullable parameter --- .../Libs/Extensions/NSDictionary+EQNExtensions.h | 4 ++++ .../Libs/Extensions/NSDictionary+EQNExtensions.m | 9 +++++++++ Sources/Earthquake Network/Models/EQNSisma.h | 2 +- Sources/Earthquake Network/Models/EQNSisma.m | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.h b/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.h index cd6c6f8..3332948 100644 --- a/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.h +++ b/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.h @@ -16,6 +16,10 @@ NS_ASSUME_NONNULL_BEGIN /// @param prettyPrint If YES, uses white space and indentation to make the output more readable. - (NSString *)eqn_jsonStringWithPrettyPrint:(BOOL)prettyPrint; +/// Returns the value associated with a given key if exists, nil otherwise. +/// @param aKey The key for which to return the corresponding value +- (nullable id)eqn_safeObjectForKey:(id)aKey; + @end NS_ASSUME_NONNULL_END diff --git a/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.m b/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.m index b85cf58..b756f6d 100644 --- a/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.m +++ b/Sources/Earthquake Network/Libs/Extensions/NSDictionary+EQNExtensions.m @@ -24,4 +24,13 @@ return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; } +- (id)eqn_safeObjectForKey:(id)aKey +{ + NSObject *object = [self objectForKey:aKey]; + if (object == [NSNull null]) { + return nil; + } + return object; +} + @end diff --git a/Sources/Earthquake Network/Models/EQNSisma.h b/Sources/Earthquake Network/Models/EQNSisma.h index 5551443..3c71307 100644 --- a/Sources/Earthquake Network/Models/EQNSisma.h +++ b/Sources/Earthquake Network/Models/EQNSisma.h @@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, strong) NSNumber *preliminary; @property (nonatomic, strong) NSNumber *smartphoneNumber; @property (nonatomic, strong) NSNumber *userNumber; -@property (nonatomic, strong) NSString *weatherCode; +@property (nonatomic, strong, nullable) NSString *weatherCode; @property (nonatomic, strong) NSString *weatherIcon; @property (nonatomic, strong) NSNumber *weatherCloud; @property (nonatomic, strong) NSNumber *weatherWindSpeed; diff --git a/Sources/Earthquake Network/Models/EQNSisma.m b/Sources/Earthquake Network/Models/EQNSisma.m index 212e220..03f33f0 100644 --- a/Sources/Earthquake Network/Models/EQNSisma.m +++ b/Sources/Earthquake Network/Models/EQNSisma.m @@ -9,6 +9,7 @@ #import "EQNSisma.h" #import "EQNUser.h" #import "EQNUtility.h" +#import "NSDictionary+EQNExtensions.h" @implementation EQNSisma @@ -42,7 +43,7 @@ self.smartphoneNumber = info[@"sm"]; self.userNumber = info[@"rp"]; - self.weatherCode = info[@"wc"]; + self.weatherCode = [info eqn_safeObjectForKey:@"wc"]; self.weatherIcon = info[@"ic"]; self.weatherCloud = info[@"cl"]; self.weatherWindSpeed = info[@"ws"];