Kostas.N
Kostas.N

Reputation: 176

Weird NSDictionary crash

I have the following code:

NSDictionary *dict = [[NSDictionary alloc]
          initWithObjectsAndKeys:myarray1, @"array1", myarray2, @"array2" nil];

NSArray *shorts =[[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];

for (NSString *dir in shorts) {
    NSArray *tempArr = [dict objectForKey:dir];
    for (NSString *file in tempArr ) {
        NSLog(@"%@", file);
    }
}

Where myarray1 and myarray2 are NSArrays.

When I execute the code the application crashes with:

-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1d134

This is apparently the tempArr, which is not recognized as an NSArray. I know that [dicFiles objectForKey:dir] returns an id type object, but as a generic type, I cannot get what I'm doing wrong.

Upvotes: 0

Views: 1148

Answers (1)

jlehr
jlehr

Reputation: 15617

You haven't included the code that initializes myarray1 and myarray2, but apparently one or both of them are instances of NSString rather than NSArray. You can check that after retrieving one of the objects from the array as follows:

if (![tempArr isKindOfClass:[NSArray class]])
{
    NSLog(@"Unable to process temp array because it's an instance of %@", [tempArr class]);
}
else
{
    // for loop code goes here...
}

Upvotes: 1

Related Questions