iOS 崩溃:“此 class 与键 url 的键值编码不兼容”
iOS Crash: " this class is not key value coding-compliant for the key url"
我在 crashlytics 上遇到以下崩溃。
Fatal Exception: NSUnknownKeyException
[<__NSCFString 0x1742aeb80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key url.
这是部分用户发生崩溃的地方。
id url = [[json[@"data"]valueForKey:@"value"]valueForKey:@"url"];
我不确定防止此崩溃的最佳方法是什么。我相信这是因为 json[@"data"]
在某些情况下是 NSString 。所以我相信我应该检查这是否是像这样的 NSDictionary。
if ([json[@"data"] isKindOfClass:[NSDictionary class]]) {
id url = [[json[@"data"]valueForKey:@"value"]valueForKey:@"url"];
}
如有任何提示或建议,我们将不胜感激。
这是我从这里得到答案后的最终结果。这看起来好吗?为了简单起见,一开始我没有包含所有代码。
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
}
}
}
你应该一一检查NSDictionary
以防止崩溃。在下面尝试我的代码
NSDictionary *dictionary = json[@"data"];
NSString *output = @"";
if ([dictionary isKindOfClass:[NSDictionary class]]) {
dictionary = dictionary[@"value"];
if ([dictionary isKindOfClass:[NSDictionary class]]) {
output = dictionary[@"url"];
}
}
NSLog(@"%@", output);
您因为在 NSString
值上调用 valueForKey
方法而崩溃。如果有人说崩溃的原因是当字典没有这个键时调用 valueForKey
,那是错误的。更多信息 Sending a message to nil in Objective-C
[dictionary isKindOfClass:[NSDictionary class]] 总是 return NO
如果 dictionary
是 nil 所以不需要检查 dictionary
if statement
。没必要。
你的错误意味着 json[@"data"]valueForKey:@"value"]
没有 NSDictionarry,所以它没有 @"url"
键。
valueForKey
这是 KVC 方法,对字典使用 objectForKey
,并添加更多检查,如:
id url = nil;
NSDictionary *data = [json objectForkey:@"data"];
if ([data isKindOfClass:[NSDictionary class]]) {
NSDictionary *value = [data objectForKey:@"value"];
if ([value isKindOfClass:[NSDictionary class]]) {
url = [value objectForKey:@"url"];
}
}
我在 crashlytics 上遇到以下崩溃。
Fatal Exception: NSUnknownKeyException [<__NSCFString 0x1742aeb80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key url.
这是部分用户发生崩溃的地方。
id url = [[json[@"data"]valueForKey:@"value"]valueForKey:@"url"];
我不确定防止此崩溃的最佳方法是什么。我相信这是因为 json[@"data"]
在某些情况下是 NSString 。所以我相信我应该检查这是否是像这样的 NSDictionary。
if ([json[@"data"] isKindOfClass:[NSDictionary class]]) {
id url = [[json[@"data"]valueForKey:@"value"]valueForKey:@"url"];
}
如有任何提示或建议,我们将不胜感激。
这是我从这里得到答案后的最终结果。这看起来好吗?为了简单起见,一开始我没有包含所有代码。
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
}
}
}
你应该一一检查NSDictionary
以防止崩溃。在下面尝试我的代码
NSDictionary *dictionary = json[@"data"];
NSString *output = @"";
if ([dictionary isKindOfClass:[NSDictionary class]]) {
dictionary = dictionary[@"value"];
if ([dictionary isKindOfClass:[NSDictionary class]]) {
output = dictionary[@"url"];
}
}
NSLog(@"%@", output);
您因为在 NSString
值上调用 valueForKey
方法而崩溃。如果有人说崩溃的原因是当字典没有这个键时调用 valueForKey
,那是错误的。更多信息 Sending a message to nil in Objective-C
[dictionary isKindOfClass:[NSDictionary class]] 总是 return NO
如果 dictionary
是 nil 所以不需要检查 dictionary
if statement
。没必要。
你的错误意味着 json[@"data"]valueForKey:@"value"]
没有 NSDictionarry,所以它没有 @"url"
键。
valueForKey
这是 KVC 方法,对字典使用 objectForKey
,并添加更多检查,如:
id url = nil;
NSDictionary *data = [json objectForkey:@"data"];
if ([data isKindOfClass:[NSDictionary class]]) {
NSDictionary *value = [data objectForKey:@"value"];
if ([value isKindOfClass:[NSDictionary class]]) {
url = [value objectForKey:@"url"];
}
}