简体   繁体   中英

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 (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:

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.

Update 2: Changed || 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'

Exception occurred because you called objectForKey method with an object type NSTaggedPointerString

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!

As users Nhat Dinh, Amit Tandel and Larme suggested, some dictionaries within rootDictCopy had been transformed into an NSString earlier in the code. I fixed that, making those dictionaries remain dictionaries, and I no longer receive that error. Thanks for everyone's help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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