Ios 未捕获异常';nsrange异常';,原因:';***-[\uu NSArrayI objectAtIndex

Ios 未捕获异常';nsrange异常';,原因:';***-[\uu NSArrayI objectAtIndex,ios,iphone,uitableview,Ios,Iphone,Uitableview,我刚接触objective C,我想知道是否有人能帮我找出为什么我的应用程序一直崩溃。我相信这与我的阵列有关,但我不知道如何修复它。有人能帮我吗 这是它崩溃时的日志输出: 2014-11-26 10:37:42.172 SamplePhotoReDo[89697:109203550] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]

我刚接触objective C,我想知道是否有人能帮我找出为什么我的应用程序一直崩溃。我相信这与我的阵列有关,但我不知道如何修复它。有人能帮我吗

这是它崩溃时的日志输出:

2014-11-26 10:37:42.172 SamplePhotoReDo[89697:109203550] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
    *** First throw call stack:
     (
0   CoreFoundation                      0x000000010c75af35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010c09cbb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010c65301e -[__NSArrayI objectAtIndex:] + 190
3   UIKit                               0x000000010d67474a -[UITableViewDataSource tableView:heightForHeaderInSection:] + 39
4   UIKit                               0x000000010d221f4e -[UITableView _delegateWantsHeaderForSection:] + 261
5   UIKit                               0x000000010d3a6983 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
6   UIKit                               0x000000010d3ace45 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 320
7   UIKit                               0x000000010d3acf3a -[UITableViewRowData heightForTable] + 56
8   UIKit                               0x000000010d1ffaf0 -[UITableView _updateContentSize] + 381
9   UIKit                               0x000000010d21cecd -[UITableView didMoveToWindow] + 65
10  UIKit                               0x000000010d1a39a0 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1482
11  UIKit                               0x000000010d1b4333 -[UIScrollView _didMoveFromWindow:toWindow:] + 55
12  UIKit                               0x000000010d1a368e -[UIView(Internal) _didMoveFromWindow:toWindow:] + 696
13  UIKit                               0x000000010d19c112 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
14  UIKit                               0x000000010d19c086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
15  UIKit                               0x000000010d1a5f4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604
16  UIKit                               0x000000010d486534 -[UINavigationTransitionView transition:fromView:toView:] + 479
17  UIKit                               0x000000010d2841ef -[UINavigationController _startTransition:fromViewController:toViewController:] + 2967
18  UIKit                               0x000000010d284487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
19  UIKit                               0x000000010d284f47 -[UINavigationController __viewWillLayoutSubviews] + 43
20  UIKit                               0x000000010d3ca509 -[UILayoutContainerView layoutSubviews] + 202
21  UIKit                               0x000000010d1a8973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
22  QuartzCore                          0x000000011052bde8 -[CALayer layoutSublayers] + 150
23  QuartzCore                          0x0000000110520a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
24  QuartzCore                          0x000000011052087e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
25  QuartzCore                          0x000000011048e63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
26  QuartzCore                          0x000000011048f74a _ZN2CA11Transaction6commitEv + 390
27  UIKit                               0x000000010d12d54d -[UIApplication _reportMainSceneUpdateFinished:] + 44
28  UIKit                               0x000000010d12e238 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642
29  UIKit                               0x000000010d12cbf2 -[UIApplication workspaceDidEndTransaction:] + 179
30  FrontBoardServices                  0x0000000112f002a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
31  CoreFoundation                      0x000000010c69053c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
32  CoreFoundation                      0x000000010c686285 __CFRunLoopDoBlocks + 341
33  CoreFoundation                      0x000000010c686045 __CFRunLoopRun + 2389
34  CoreFoundation                      0x000000010c685486 CFRunLoopRunSpecific + 470
35  UIKit                               0x000000010d12c669 -[UIApplication _run] + 413
36  UIKit                               0x000000010d12f420 UIApplicationMain + 1282
37  SamplePhotoReDo                     0x000000010bb60453 main + 115
38  libdyld.dylib                       0x000000010f2d2145 start + 1
 )
  libc++abi.dylib: terminating with uncaught exception of type NSException
   (lldb)'
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 + self.collectionsFetchResults.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if (section == 0) {
        numberOfRows = 1; // "All Photos" section
    } else {
        PHFetchResult *fetchResult = self.collectionsFetchResults[section - 1];
        numberOfRows = fetchResult.count;
    }
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *localizedTitle = nil;

    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:AllPhotosReuseIdentifier forIndexPath:indexPath];
        localizedTitle = NSLocalizedString(@"All Photos", @"");
     } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CollectionCellReuseIdentifier forIndexPath:indexPath];
        PHFetchResult *fetchResult = self.collectionsFetchResults[indexPath.section - 1];
        PHCollection *collection = fetchResult[indexPath.row];
        localizedTitle = collection.localizedTitle;
    }
    cell.textLabel.text = localizedTitle;

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    if (section > 0) {
        title = self.collectionsLocalizedTitles[section - 1];
    }
    return title;
}

#pragma mark - PHPhotoLibraryChangeObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    // Call might come on any background queue. Re-dispatch to the main queue to handle it.
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray *updatedCollectionsFetchResults = nil;
        for (PHFetchResult *collectionsFetchResult in self.collectionsFetchResults) {
            PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:collectionsFetchResult];
            if (changeDetails) {
                if (!updatedCollectionsFetchResults) {
                    updatedCollectionsFetchResults = [self.collectionsFetchResults mutableCopy];
                }
            [updatedCollectionsFetchResults replaceObjectAtIndex:[self.collectionsFetchResults indexOfObject:collectionsFetchResult] withObject:[changeDetails fetchResultAfterChanges]];
        }
    }
    if (updatedCollectionsFetchResults) {
        self.collectionsFetchResults = updatedCollectionsFetchResults;
        [self.tableView reloadData];
    }
    });
}
以下是可能导致崩溃的代码:

2014-11-26 10:37:42.172 SamplePhotoReDo[89697:109203550] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
    *** First throw call stack:
     (
0   CoreFoundation                      0x000000010c75af35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010c09cbb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010c65301e -[__NSArrayI objectAtIndex:] + 190
3   UIKit                               0x000000010d67474a -[UITableViewDataSource tableView:heightForHeaderInSection:] + 39
4   UIKit                               0x000000010d221f4e -[UITableView _delegateWantsHeaderForSection:] + 261
5   UIKit                               0x000000010d3a6983 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
6   UIKit                               0x000000010d3ace45 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 320
7   UIKit                               0x000000010d3acf3a -[UITableViewRowData heightForTable] + 56
8   UIKit                               0x000000010d1ffaf0 -[UITableView _updateContentSize] + 381
9   UIKit                               0x000000010d21cecd -[UITableView didMoveToWindow] + 65
10  UIKit                               0x000000010d1a39a0 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1482
11  UIKit                               0x000000010d1b4333 -[UIScrollView _didMoveFromWindow:toWindow:] + 55
12  UIKit                               0x000000010d1a368e -[UIView(Internal) _didMoveFromWindow:toWindow:] + 696
13  UIKit                               0x000000010d19c112 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
14  UIKit                               0x000000010d19c086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
15  UIKit                               0x000000010d1a5f4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604
16  UIKit                               0x000000010d486534 -[UINavigationTransitionView transition:fromView:toView:] + 479
17  UIKit                               0x000000010d2841ef -[UINavigationController _startTransition:fromViewController:toViewController:] + 2967
18  UIKit                               0x000000010d284487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
19  UIKit                               0x000000010d284f47 -[UINavigationController __viewWillLayoutSubviews] + 43
20  UIKit                               0x000000010d3ca509 -[UILayoutContainerView layoutSubviews] + 202
21  UIKit                               0x000000010d1a8973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
22  QuartzCore                          0x000000011052bde8 -[CALayer layoutSublayers] + 150
23  QuartzCore                          0x0000000110520a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
24  QuartzCore                          0x000000011052087e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
25  QuartzCore                          0x000000011048e63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
26  QuartzCore                          0x000000011048f74a _ZN2CA11Transaction6commitEv + 390
27  UIKit                               0x000000010d12d54d -[UIApplication _reportMainSceneUpdateFinished:] + 44
28  UIKit                               0x000000010d12e238 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642
29  UIKit                               0x000000010d12cbf2 -[UIApplication workspaceDidEndTransaction:] + 179
30  FrontBoardServices                  0x0000000112f002a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
31  CoreFoundation                      0x000000010c69053c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
32  CoreFoundation                      0x000000010c686285 __CFRunLoopDoBlocks + 341
33  CoreFoundation                      0x000000010c686045 __CFRunLoopRun + 2389
34  CoreFoundation                      0x000000010c685486 CFRunLoopRunSpecific + 470
35  UIKit                               0x000000010d12c669 -[UIApplication _run] + 413
36  UIKit                               0x000000010d12f420 UIApplicationMain + 1282
37  SamplePhotoReDo                     0x000000010bb60453 main + 115
38  libdyld.dylib                       0x000000010f2d2145 start + 1
 )
  libc++abi.dylib: terminating with uncaught exception of type NSException
   (lldb)'
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 + self.collectionsFetchResults.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if (section == 0) {
        numberOfRows = 1; // "All Photos" section
    } else {
        PHFetchResult *fetchResult = self.collectionsFetchResults[section - 1];
        numberOfRows = fetchResult.count;
    }
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *localizedTitle = nil;

    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:AllPhotosReuseIdentifier forIndexPath:indexPath];
        localizedTitle = NSLocalizedString(@"All Photos", @"");
     } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CollectionCellReuseIdentifier forIndexPath:indexPath];
        PHFetchResult *fetchResult = self.collectionsFetchResults[indexPath.section - 1];
        PHCollection *collection = fetchResult[indexPath.row];
        localizedTitle = collection.localizedTitle;
    }
    cell.textLabel.text = localizedTitle;

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    if (section > 0) {
        title = self.collectionsLocalizedTitles[section - 1];
    }
    return title;
}

#pragma mark - PHPhotoLibraryChangeObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    // Call might come on any background queue. Re-dispatch to the main queue to handle it.
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray *updatedCollectionsFetchResults = nil;
        for (PHFetchResult *collectionsFetchResult in self.collectionsFetchResults) {
            PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:collectionsFetchResult];
            if (changeDetails) {
                if (!updatedCollectionsFetchResults) {
                    updatedCollectionsFetchResults = [self.collectionsFetchResults mutableCopy];
                }
            [updatedCollectionsFetchResults replaceObjectAtIndex:[self.collectionsFetchResults indexOfObject:collectionsFetchResult] withObject:[changeDetails fetchResultAfterChanges]];
        }
    }
    if (updatedCollectionsFetchResults) {
        self.collectionsFetchResults = updatedCollectionsFetchResults;
        [self.tableView reloadData];
    }
    });
}

很明显,你的数组没有你想象的那么多元素。在数组填充的地方放一个断点,并仔细检查它是否如预期的那样。

似乎你试图用无效的索引/偏移量访问数组。你是否已经尝试减少问题?哪一行会向你抛出异常?数组甚至看起来都是空的。感谢你的快速回复!正如我前面提到的,我对objective C和应用程序开发是新手。你介意告诉我需要在哪里添加断点吗?再次感谢你谢谢你,沃尔夫!我能做些什么来解决这个问题?你能提供脚本来纠正它吗..我怀疑我能,这这是一个苹果的例子,我不知道你对它做了什么。在这里列出你正在使用的所有阵列,我会告诉你哪一个是错误的。再次感谢你,latenitecoder!我真的很感谢大家的帮助。实际上,你提供的链接是我为了学习而试图复制的应用程序。我还没有做任何更改,因为我不知道从这里该怎么办。。