简体   繁体   English

比较两个字典值会给我一个NSInvalidArgumentException

[英]Comparing two dictionary values gives me an NSInvalidArgumentException

I have a for loop that iterates through dictionaries within a dictionary, trying to find one that has a key that matches a key from a completely separate dictionary. 我有一个for循环,该循环遍历字典中的字典,试图找到一个具有与完全独立的字典中的键相匹配的键的键。

for (id rDCKey in rootDictCopy)
{
    tempHouseNumber = rDCKey;

    if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
    {
        NSLog(@"done");
        goto DONE;
    }
}

When both values are equal to nothing, it's fine, and everything passes. 当两个值都等于零时,就可以了,一切都通过了。 But the moment they have a value (which is always a 256 character long NSString), it crashes, giving me this error: 但是,一旦它们具有值(通常为256个字符长的NSString),它就会崩溃,并给我这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000333939394'

I have no clue what's up, and any help would be appreciated. 我不知道怎么回事,任何帮助将不胜感激。

I can give more code if necessary. 如果需要,我可以提供更多代码。

Thanks. 谢谢。

Update: I updated my for loop to check for types, but the same problem occurs. 更新:我更新了for循环以检查类型,但是发生相同的问题。

Update 2: Changed || 更新2:已更改|| to && 至 &&

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:]:无法识别的选择器已发送到实例0xa000000333939394'

Exception occurred because you called objectForKey method with an object type NSTaggedPointerString 发生异常是因为您使用对象类型NSTaggedPointerString调用了objectForKey方法

Before compare you should check type of your data. 比较之前,您应该检查数据类型。 You can do as below: 您可以执行以下操作:

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
} 

And your code should be like below: 您的代码应如下所示:

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;
    }
}

Note: My answer will help your code work without crash but your should find why you got unexpected object type here. 注意:我的回答将帮助您的代码正常运行而不会崩溃,但是您应该在这里找到为什么得到意外的对象类型。 And how to sure you alway recevied NSMutableDictionary object, that is the best solution! 以及如何确保始终删除NSMutableDictionary对象,那是最好的解决方案!

As users Nhat Dinh, Amit Tandel and Larme suggested, some dictionaries within rootDictCopy had been transformed into an NSString earlier in the code. 正如用户Nhat Dinh,Amit Tandel和Larme所建议的那样,rootDictCopy中的某些词典已在代码的早期转换为NSString。 I fixed that, making those dictionaries remain dictionaries, and I no longer receive that error. 我已修复该问题,使这些词典仍然是词典,并且不再收到该错误。 Thanks for everyone's help! 感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM