iPhone app crash [__NSCFString objectForKey:] unrecognized selector sent to instance

女生的网名这么多〃 提交于 2019-11-28 10:50:42

问题


I am trying to add in tempArray only even number of data.localBookmarks is array of dictionary. Here is my code :

currentIndex = indexPath.row;
for (NSDictionary *dict in localBookmarks)
    {
    if (currentIndex % 2 == 0 && currentIndex <= [localBookmarks count])
     {             
           [tempArray addObject:[dict objectForKey:@"firstName"]];
     }
        currentIndex++;
    }
NSLog(@"tempArray %@",tempArray);

cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];
return cell;

my app crash on [tempArray addObject:[dict objectForKey:@"firstName"]]; this line ,How can I fix it?


回答1:


Why you are crashing

The variable dict that you think is a NSDictionary is actually a NSString. Since strings don't respond to the objectForKey: method your app crashed. That is what the error message is telling you.

Where the problem lies

Your statement: "data.localBookmarks is array of dictionary" is false. At least one of them is just a string.

Check where your localBookmarks comes from. If it's data you are parsing you may need to change that.



来源:https://stackoverflow.com/questions/15787876/iphone-app-crash-nscfstring-objectforkey-unrecognized-selector-sent-to-inst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!