I have a class that I use to setup objects in an array. In this class I have a custom "initWithDictionary", where I parse a JSON
dictionary. However, as I am running into NSNull
, this crashes my app. To get around this, I set up a class that handles exceptions, so when a string is NSNull
, it's replace it with @""
. or -1 for integers.
This is my NullExtensions class:
@interface NSNull (valueExtensions)
-(int)intValue;
-(NSString *)stringValue;
@end
@implementation NSNull (valueExtensions)
-(int)intValue {
return -1;
}
-(NSString*)stringValue {
return @"";
}
@end
However, in my initWithDictionary method, the following code crashes my app:
self.bookTitle = [[parsedDictionary objectForKey:@"book_title"] stringValue];
It doesn't work regardless of the object in the parsed dictionary being NSNull
or containing a valid string. Only if I do the following (and the string is not null):
self.bookTitle = [parsedDictionary objectForKey:@"book_title"];
Is stringValue
incorrect in this case? And if so, how do I use it properly in order to setup proper NSNull
replacements?
Thx