终止应用程序由于未捕获的异常“NSRangeException”,原因
问题描述:
我使用搜索栏收集,当我做过滤文字给我的错误,如终止应用程序由于未捕获的异常“NSRangeException”,原因
` Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 37]'
*** First throw call stack:
` 这里是我的代码
- (void)filterListForSearchText:(NSString *)searchText
{
for (NSString *title in _arrayCCName) {
NSRange nameRange = [title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[_searchResultName addObject:title];
}
}
for (int i=0; i<_searchResultName.count; i++) {
NSString *str=[ObjCls objectAtIndex:i];
NSInteger index=[_searchResultName indexOfObject:str];
[_searchResultDeignation addObject:[_arrayCCDesignation objectAtIndex:index]];
[_searchResultProfilePicture addObject:[_arrayCCProfilePicture objectAtIndex:index]];
[_searchResultFamilyPicture addObject:[_arrayCCFamilyPicture objectAtIndex:index]];
NSLog(@"array index is %ld",(long)index);
}
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
SEARCHBAR.showsCancelButton=NO;
[self.view endEditing:YES];
}
我得到了上面的错误,请帮助我如何解决?
答
简单意味着索引值大于数组在某个点的大小。当使用[array objecAtIndex:index]时,索引必须小于array.count。
答
这里这些行....
NSInteger index=[_searchResultName indexOfObject:str];
[_searchResultDeignation addObject:[_arrayCCDesignation objectAtIndex:index]];
[_searchResultProfilePicture addObject:[_arrayCCProfilePicture objectAtIndex:index]];
[_searchResultFamilyPicture addObject:[_arrayCCFamilyPicture objectAtIndex:index]];
NSLog(@"array index is %ld",(long)index);
你假设indexOfObject居然发现的东西。如果str
不存在
从资料为准....
宣言,会发生什么 Objective-C的 - (NSUInteger)indexOfObject:(ID)anObject 参数 anObject
的对象。 返回值 对应数组值等于anObject的最低索引。如果数组中没有任何对象等于anObject,则返回NSNotFound。
添加异常断点。从Mian菜单调试:断点:创建异常断点。运行应用程序以获取断点。当你点击异常断点**点击debug continue **几次,你会得到一个回溯和更多的错误信息。发布它和Xcode/Debugger消息的精确副本。 – zaph 2014-11-05 12:42:29
也许你的'index'大于'_arrayCCDesignation.count'或'_arrayCCProfilePicture.count'或'_arrayCCFamilyPicture.count' – 2014-11-05 13:00:25
什么是'ObjCls'? – trojanfoe 2014-11-05 13:02:09