如果在数组中找不到任何内容,为什么此循环会导致崩溃

用户名

我完全迷失了方向,甚至不确定如何表达这个问题。我有一个循环,如下所示。如果if循环中语句证明为trueindexPath分配了值,则循环完成(_samplePictures数组中有49个对象if (found),然后输入,然后加载找到的数据。如果条件不成立,并且未找到任何内容而不是进入if(!found)应用程序,则崩溃。我已经尝试了这种当前编码方案的各种结构。我什至删除了循环,并输入了if(!found)它应该执行的操作。(ooooodddddddd)

该语句位于的viewDidLoad正上方(if语句不是viewDidLoad),它是if确定的内容的语句_samplePictures

知道数组中的图像定义如下可能很有用:

#define IMG_71          [UIImage imageNamed:@"imagename.jpg"]

数组的结构如下:

_samplePictures = @[@{@"Image": IMG_71, @"Title" : @"title of item", @"Description" : @"complete description of item"}];

if (fromKitInstructions)
{
    int i = 0;

    NSIndexPath *indexPath;
    found = FALSE;

    while(i <= _samplePictures.count)
    {
        if([_samplePictures[i] objectForKey:@"Title"] == instructionName)
        {
            indexPath  = [NSIndexPath indexPathForRow:i inSection:0];
            i = 50;
            found = TRUE;
        }
        else
        {
            i++;
        }
    }

    if(found)
    {
        [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    }

    if(!found)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:instructionName message:@"We are sorry but the Kit ID you searched for could not be found. Press the Back button to return to the Instructions you were viewing." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Say Hello",nil];
        [alert show];
    }
}

这是使您进入此代码的按钮:

-(void)goTo:(id)sender
{
    ViewController *goGallery = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
    goGallery.setFlag = NO;
    goGallery.fromKitInstructions = YES;
    goGallery.instructionName = instructionName;
    [self.navigationController pushViewController:goGallery animated:YES];

}

调试器显示以下内容:

2014-05-30 15:08:37.873 TechBook[3932:60b] [INFO] <HomeViewController: 0xc67bb00> loaded
2014-05-30 15:14:24.268 TechBook[3932:3c03] void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForMIMEType:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
2014-05-30 15:14:43.204 TechBook[3932:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 49 beyond bounds [0 .. 48]'
*** First throw call stack:
(
    0   CoreFoundation                      0x01f3d1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01cbc8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01ef18b2 -[__NSArrayI objectAtIndex:] + 210
    3   CoreFoundation                      0x01fbbf48 -[NSArray objectAtIndexedSubscript:] + 40
    4   TechBook                            0x0000ae3d -[ViewController viewDidLoad] + 22381
    5   UIKit                               0x0088e33d -[UIViewController loadViewIfRequired] + 696
    6   UIKit                               0x0088e5d9 -[UIViewController view] + 35
    7   UIKit                               0x008a8942 -[UINavigationController _startCustomTransition:] + 778
    8   UIKit                               0x008b58f7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
    9   UIKit                               0x008b64e9 -[UINavigationController __viewWillLayoutSubviews] + 57
    10  UIKit                               0x009f70d1 -[UILayoutContainerView layoutSubviews] + 213
    11  UIKit                               0x007de964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    12  libobjc.A.dylib                     0x01cce82b -[NSObject performSelector:withObject:] + 70
    13  QuartzCore                          0x001f145a -[CALayer layoutSublayers] + 148
    14  QuartzCore                          0x001e5244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    15  QuartzCore                          0x001e50b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    16  QuartzCore                          0x0014b7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    17  QuartzCore                          0x0014cb85 _ZN2CA11Transaction6commitEv + 393
    18  QuartzCore                          0x0020a5b0 +[CATransaction flush] + 52
    19  UIKit                               0x0076d9bb _UIApplicationHandleEventQueue + 13095
    20  CoreFoundation                      0x01ec677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    21  CoreFoundation                      0x01ec610b __CFRunLoopDoSources0 + 235
    22  CoreFoundation                      0x01ee31ae __CFRunLoopRun + 910
    23  CoreFoundation                      0x01ee29d3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x01ee27eb CFRunLoopRunInMode + 123
    25  GraphicsServices                    0x036d75ee GSEventRunModal + 192
    26  GraphicsServices                    0x036d742b GSEventRun + 104
    27  UIKit                               0x0076ff9b UIApplicationMain + 1225
    28  TechBook                            0x00003b1d main + 141
    29  libdyld.dylib                       0x027ee701 start + 1
    30  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Stonz2
while(i <= _samplePictures.count)

更改为:

while(i < _samplePictures.count)

数组从0-48运行,但_sampePictures.count返回总共49个对象。因此,当您遍历数组的最后一次运行时,计数总数大于最终索引。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如果在数组中找不到任何内容,为什么此循环会导致崩溃

来自分类Dev

如果在数组中找不到值,则从字典中删除对象

来自分类Dev

jQuery添加元素,如果在数组中找不到

来自分类Dev

如果在数组中找不到值,则返回 NULL

来自分类Dev

为什么此正则表达式在本文中找不到任何内容?

来自分类Dev

如果在 WHERE IN 子句中找不到任何值的值,则返回 0

来自分类Dev

为什么此RegEx找不到任何数据?

来自分类Dev

如果FIND函数在vba中找不到任何内容

来自分类Dev

为什么我的for循环找不到任何文件?

来自分类Dev

如果在查询集中找不到任何内容,如何将用户重定向到其他一些 views.py?

来自分类Dev

如果找不到任何内容,则Object.get()会引发异常

来自分类Dev

如果找不到任何内容,搜索方法应该返回什么?

来自分类Dev

为什么此Google AppEngine搜索API查询找不到任何结果?

来自分类Dev

如果在MYSQL中找不到匹配项,则die()

来自分类Dev

AngularJS显示元素(如果在数组中)

来自分类Dev

AngularJS显示元素(如果在数组中)

来自分类Dev

显示角度(如果在数组中)

来自分类Dev

如果在angularjs中找不到数据,如何在数据表中显示空消息

来自分类Dev

如果在迭代时从列表中删除元素,为什么会跳过元素?

来自分类Dev

如果在 tkinter 中单击按钮之前的语句,为什么 python 会执行

来自分类Dev

如果在 dealloc 之前没有删除 KVO 观察者,为什么应用程序会崩溃?

来自分类Dev

如果在树中找不到输入,如何使此递归二叉树遍历返回null?

来自分类Dev

为什么apt找不到任何包的源代码?

来自分类Dev

为什么apt找不到任何包的源代码?

来自分类Dev

为什么找不到任何手册页?

来自分类Dev

如果在写之前打开Lucene DirectoryReader,为什么看不到IndexWriter所做的任何更改?

来自分类Dev

在Kendo UI组合框中找不到任何内容

来自分类Dev

系统冻结。在日志中找不到任何内容

来自分类Dev

Ansible 在存储库中找不到任何内容

Related 相关文章

  1. 1

    如果在数组中找不到任何内容,为什么此循环会导致崩溃

  2. 2

    如果在数组中找不到值,则从字典中删除对象

  3. 3

    jQuery添加元素,如果在数组中找不到

  4. 4

    如果在数组中找不到值,则返回 NULL

  5. 5

    为什么此正则表达式在本文中找不到任何内容?

  6. 6

    如果在 WHERE IN 子句中找不到任何值的值,则返回 0

  7. 7

    为什么此RegEx找不到任何数据?

  8. 8

    如果FIND函数在vba中找不到任何内容

  9. 9

    为什么我的for循环找不到任何文件?

  10. 10

    如果在查询集中找不到任何内容,如何将用户重定向到其他一些 views.py?

  11. 11

    如果找不到任何内容,则Object.get()会引发异常

  12. 12

    如果找不到任何内容,搜索方法应该返回什么?

  13. 13

    为什么此Google AppEngine搜索API查询找不到任何结果?

  14. 14

    如果在MYSQL中找不到匹配项,则die()

  15. 15

    AngularJS显示元素(如果在数组中)

  16. 16

    AngularJS显示元素(如果在数组中)

  17. 17

    显示角度(如果在数组中)

  18. 18

    如果在angularjs中找不到数据,如何在数据表中显示空消息

  19. 19

    如果在迭代时从列表中删除元素,为什么会跳过元素?

  20. 20

    如果在 tkinter 中单击按钮之前的语句,为什么 python 会执行

  21. 21

    如果在 dealloc 之前没有删除 KVO 观察者,为什么应用程序会崩溃?

  22. 22

    如果在树中找不到输入,如何使此递归二叉树遍历返回null?

  23. 23

    为什么apt找不到任何包的源代码?

  24. 24

    为什么apt找不到任何包的源代码?

  25. 25

    为什么找不到任何手册页?

  26. 26

    如果在写之前打开Lucene DirectoryReader,为什么看不到IndexWriter所做的任何更改?

  27. 27

    在Kendo UI组合框中找不到任何内容

  28. 28

    系统冻结。在日志中找不到任何内容

  29. 29

    Ansible 在存储库中找不到任何内容

热门标签

归档