user2213271
user2213271

Reputation: 421

__NSCFString objectForKeyedSubscript: exception

I take datas from server. My app work fine in Sinulator and test device iPhone 4s, but one man have problem on iPod 4. He get exception:

-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1d263a20

I cann't use this device so I write code to know where crash was.

 if (![dictionaryRest[@"compliments"] isEqual:[NSNull null]]) {
       NSMutableArray *array = [NSMutableArray new];
       NSMutableArray *firstArray = [NSMutableArray new];
       for (NSDictionary *dic in dictionaryRest[@"compliments"]) {
            Compliment *compl = [Compliment new];
            if (![dic[@"ID_promotions"] isEqual:[NSNull null]])
                compl.ID = [dic[@"ID_promotions"] integerValue];

So in last 2 strings this exception was. What the reason of this? So I understand that I need use

if ([dict objectForKey:[@"compliments"])

instead

if (![dict[@"compliments"] isEqual:[NSNull null]])

and in all another cases.

I test now and I have in my dictionary for ID: enter image description here

Upvotes: 4

Views: 9946

Answers (2)

govind sah
govind sah

Reputation: 21

-objectForKeyedSubscript: is an instance method on NSDictionary object. __NSCFString objectForKeyedSubscript: exception indicates that the method -objectForKeyedSubscript is somehow getting called on some NSString object. So basically you just have to properly check for the class of the object before you can safely assume that it is actually dictionary.

if([dic isKindOfClass:[NSDictionary class]]) 
{
    id obj = dic[@"key"];
}

Upvotes: 0

bbum
bbum

Reputation: 162722

You have an NSString instance in your dictionary where you expect a dictionary.

Note that your "use this instead of that" has nothing to do with the problem.

Upvotes: 15

Related Questions