gpt4 book ai didi

ios - 非空数组出现越界错误 - 发生了什么?

转载 作者:行者123 更新时间:2023-11-29 03:18:17 24 4
gpt4 key购买 nike

我正在尝试实现一个 searchResultsTableView,它将根据用户的搜索重新加载表格。在我知道会抛出异常的 numberOfRowsInSection 函数中,我有以下内容:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return self.objects.count;
} else {
NSLog(@"The count of self.searchresults is %i", self.searchResults.count); //Shows as non-zero
return self.searchResults.count;
}

}

令我感到困惑的是,我在 return 语句之前设置了一个断点,而且我知道我得到的是基于 NSLog 的非 0 结果。返回非 0 数字后立即抛出异常,我得到异常“index 0 beyond bounds for empty array”

我做错了什么?

我还确保只手动重新加载表,所以在 shouldReloadTableForSearchString 中,我有以下方法:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

if (searchString.length > 3){
[self.searchResults removeAllObjects];

PFQuery *query = [PFUser query];
[query whereKey:@"username" containsString:searchString];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"There is an error");
} else {
[self.searchResults addObjectsFromArray:objects];
NSLog(@"self.resultResults count %i", self.searchResults.count);
NSLog(@"The objects are %@", self.searchResults);
[self.searchDisplayController.searchResultsTableView reloadData];
}
}];

}
return NO;
}

谢谢!

最佳答案

看来你的应用程序没有崩溃

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

检查您在哪里调用 objectAtIndex: 方法,看看您是否正在对无效索引进行调用。

关于ios - 非空数组出现越界错误 - 发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com