异常'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 19 超出范围 [0 .. 18]'
Posted
技术标签:
【中文标题】异常\'NSRangeException\',原因:\'*** -[__NSArrayM objectAtIndex:]:索引 19 超出范围 [0 .. 18]\'【英文标题】:Exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 19 beyond bounds [0 .. 18]'异常'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 19 超出范围 [0 .. 18]' 【发布时间】:2014-03-07 20:31:05 【问题描述】:我已经寻找过其他类似的帖子,但没有一个可以解决我的问题。我收到此错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 19 beyond bounds [0 .. 18]'
我正在使用搜索栏和显示控制器从 PARSE.COM 数据库中获取数据。
这是我的代码:
#import "busquedaViewController.h"
@interface busquedaViewController ()
@end
@implementation busquedaViewController
@synthesize totaldetalles;
@synthesize resultados;
@synthesize busquedaTableView;
- (id)initWithStyle:(UITableViewStyle)style
self = [super initWithStyle:style];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
-(void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
// [self.activityIndicatorView startAnimating];
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
// [self.resultados removeAllObjects];
PFQuery *busqueda = [PFQuery queryWithClassName:@"icd_detalle"];
[busqueda whereKey:@"DESC_ES" containsString:[searchText uppercaseString]];
[busqueda findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error)
if(!error)
resultados = [[NSMutableArray alloc] initWithArray:results];
NSLog(@"ACA LA DATA YA FILTRADA ...%@ ... TADA", resultados);
else
NSLog(@"error en la busqueda");
];
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if (tableView == self.tableView)
return 1;
else
return self.resultados.count;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 80;
- (void)callbackLoadObjectsFromParse:(NSArray *)results error:(NSError *)error
if (!error)
[self.resultados removeAllObjects];
[self.resultados addObjectsFromArray:results];
[self.searchDisplayController.searchResultsTableView reloadData];
else
NSLog(@"error: %@ %@", error, [error userInfo]);
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *Identifier =@"bus1";
busquedaCell *cell = (busquedaCell *)[self.tableView dequeueReusableCellWithIdentifier:Identifier];
// busquedaCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil)
cell= [[busquedaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
if (tableView == self.tableView)
cell.busquedaDESC.text = @"Introdusca Busqueda";
else if (tableView == self.searchDisplayController.searchResultsTableView)
PFObject *tempDict = [self.resultados objectAtIndex:indexPath.row];
cell.busquedaDESC.text = [tempDict objectForKey:@"DESC_ES"];
cell.busquedaCode.text = [tempDict objectForKey:@"COD_4"];
// [self.activityIndicatorView stopAnimating];
return cell;
@end
这是堆栈:
*** First throw call stack:
(
0 CoreFoundation 0x00000001028ac795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010260f991 objc_exception_throw + 43
2 CoreFoundation 0x00000001028528e5 -[__NSArrayM objectAtIndex:] + 213
3 icd2 0x0000000100006956 -[busquedaViewController tableView:cellForRowAtIndexPath:] + 614
4 UIKit 0x0000000100df6b8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
5 UIKit 0x0000000100dde836 -[UITableView _updateVisibleCellsNow:] + 2297
6 UIKit 0x0000000100def381 -[UITableView layoutSubviews] + 207
7 UIKit 0x0000000100d86b27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
8 QuartzCore 0x000000010098ba22 -[CALayer layoutSublayers] + 151
9 QuartzCore 0x0000000100980589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
10 QuartzCore 0x000000010098040a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
11 QuartzCore 0x00000001008f5694 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
12 QuartzCore 0x00000001008f670c _ZN2CA11Transaction6commitEv + 394
13 QuartzCore 0x00000001009ae3bf _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 485
14 CoreFoundation 0x000000010286ee24 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
15 CoreFoundation 0x000000010286e9a2 __CFRunLoopDoTimer + 962
16 CoreFoundation 0x000000010285796e __CFRunLoopRun + 1614
17 CoreFoundation 0x0000000102856f33 CFRunLoopRunSpecific + 467
18 GraphicsServices 0x00000001045833a0 GSEventRunModal + 161
19 UIKit 0x0000000100d2b043 UIApplicationMain + 1010
20 icd2 0x00000001000051a3 main + 115
21 libdyld.dylib 0x00000001032135fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
【问题讨论】:
你从哪个方法得到这个异常? 很明显,您正在访问数组末尾之外的索引 1。错误发生在哪一行?请记住,数组以索引 0 开始,以长度为负 1 的索引结束。您正在尝试访问具有 19 个元素的对象的索引 19,因此最后一个有效索引是 18。 你读过错误信息吗?? (以及异常堆栈跟踪在哪里?异常发生在哪里??) 我刚刚添加了堆栈。它发生在搜索完成并且我向下滚动时。我有时会发生,但并非总是如此。 @charliemolain 你的问题有解决方案吗? 【参考方案1】:此异常是因为您更新了findObjectsInBackgroundWithBlock
中的数组,但tableView
正在主线程callbackLoadObjectsFromParse
上更新。
tableView
有 19 行,但数组只有 18。
【讨论】:
我只是“评论”了 callbackLoadObjectsFromParse,我得到了同样的错误。我认为是因为索引的数量,但是,如果我从“结果”数组中拖动它,我不明白为什么它会有所不同。 太棒了!它必须与 **findoObjectsinBrackgroundWithTarget 一起使用。现在我无法在 tableview 中显示数据,但我想那是另一个问题。以上是关于异常'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 19 超出范围 [0 .. 18]'的主要内容,如果未能解决你的问题,请参考以下文章
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围”
由于未捕获的异常“NSRangeException”而终止应用程序,原因:-[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“-[__NSCFArray objectAtIndex:]: index (14) beyond bounds (14)”
*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0
由于未捕获的异常“NSRangeException”而终止应用程序,原因:-[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围-2
*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds fo