I am getting the following crash on crashlytics.
Fatal Exception: NSUnknownKeyException [<__NSCFString 0x1742aeb80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key url.
This is where the crash occurred for some of the users.
id url = [[json[@"data"]valueForKey:@"value"]valueForKey:@"url"];
I'm not sure what is the best way to prevent this crash. I believe this is because json[@"data"]
is an NSString in certain cases. So I believe I should check if this is an NSDictionary like this.
if ([json[@"data"] isKindOfClass:[NSDictionary class]]) {
id url = [[json[@"data"]valueForKey:@"value"]valueForKey:@"url"];
}
Any tips or suggestions are appreciated.
This is my end result after getting answers from here. Does this look okay? I didn't include all my code at first to keep things simple.
if ([json isKindOfClass:[NSDictionary class]]) {
id url = nil;
id type = nil;
NSDictionary *data = json[@"data"];
if ([data isKindOfClass:[NSDictionary class]]) {
type = data[@"type"];
NSDictionary *value = data[@"value"];
if ([value isKindOfClass:[NSArray class]]) {
url = [value valueForKey:@"url"];
}
if ([type isKindOfClass:[NSString class]] && [url isKindOfClass:[NSArray class]] && [url count] != 0) {
// do stuff
}
}
}