Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...