由于未捕获的异常'NSRangeException'终止应用程序,原因

时间:2014-11-05 12:39:17

标签: ios

我在收集中使用搜索栏,当我在做过滤文本时,我得到了错误,如

` 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];

}

我遇到了上述错误,请帮我解决一下这个问题?

2 个答案:

答案 0 :(得分:0)

简单地表示index的值在某个时刻大于数组的大小。使用 [array objecAtIndex:index] 时,索引必须小于array.count。

答案 1 :(得分:0)

这些行......

    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