我有一个 for 循环,它遍历字典中的字典,试图找到一个键与完全独立的字典中的键匹配的键。
for (id rDCKey in rootDictCopy)
{
tempHouseNumber = rDCKey;
if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
{
NSLog(@"done");
goto DONE;
}
}
当两个值都不等于任何值时,就很好,一切都会过去。但是当它们有一个值(总是 256 个字符长的 NSString)时,它就会崩溃,给我这个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000333939394'
我不知道发生了什么,如果有任何帮助,我们将不胜感激。
如果需要,我可以提供更多代码。
谢谢。
更新:我更新了 for 循环来检查类型,但出现了同样的问题。
更新 2:更改 ||到 &&
for (id rDCKey in rootDictCopy)
{
tempHouseNumber = rDCKey;
if ([[rootDictCopy objectForKey:rDCKey] isKindOfClass:[NSMutableDictionary class]] && [[routePathRootDictCopy objectForKey:houseNumber] isKindOfClass:[NSMutableDictionary class]])
{
if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
{
NSLog(@"done");
goto DONE;
}
}
else
{
NSLog(@"ERROR");
}
}
最佳答案
reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000333939394'
发生异常是因为您使用对象类型 NSTaggedPointerString
调用了 objectForKey
方法
在比较之前,您应该检查数据的类型。 您可以执行以下操作:
if ( [obj isKindOfClass:[NSDictionary class]] ) {
// is a NSDictionary
// do further action like get objectForKey, compare ..
} else {
// you don't got what you want -> print error log or something like that
}
你的代码应该如下所示:
for (id rDCKey in rootDictCopy)
{
tempHouseNumber = rDCKey;
// TODO:
// check if rootDictCopy is a NSDictionary (if needed)
// check if routePathRootDictCopy is a NSDictionary (if needed)
// check if [rootDictCopy objectForKey:rDCKey] is a NSDictionary
// check if [routePathRootDictCopy objectForKey:houseNumber] is a NSDictionary
if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
{
NSLog(@"done");
goto DONE;
}
}
注意:我的回答将帮助您的代码在不崩溃的情况下运行,但您应该在此处找到意外对象类型的原因。以及如何确保你总是收到 NSMutableDictionary
对象,这是最好的解决方案!
关于ios - 比较两个字典值给我一个 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42061933/