NSRangeException',原因:'*** -[__NSArrayI objectAtIndex:]:索引0超出了空数组的界限。

时间:2022-08-30 14:00:54

Well, I know the cause of the error, my question here is how to avoid this kind of error. In my case, what I want is, if the user enters a wrong id which means when the array return 0 results, is says for example :

我知道错误的原因,我的问题是如何避免这种错误。在我的例子中,我想要的是,如果用户输入了一个错误的id,这意味着当数组返回0结果时,例如:

NSLog (@"The id you entered is false !");

else, it proceeds the code normally without crashing.

否则,代码通常不会崩溃。

Any ideas on how I can solve it ?

有什么办法可以解决吗?

1 个解决方案

#1


1  

id array = [dataDictionary objectForKey:@"routes"];
if([array count] == 0) {
    NSLog(@"don't access it as the index is out of bounds);
    return;
}

#1


1  

id array = [dataDictionary objectForKey:@"routes"];
if([array count] == 0) {
    NSLog(@"don't access it as the index is out of bounds);
    return;
}