NSArrayM objectAtIndex:索引 6 超出范围

Posted

技术标签:

【中文标题】NSArrayM objectAtIndex:索引 6 超出范围【英文标题】:NSArrayM objectAtIndex: index 6 beyond bounds 【发布时间】:2016-11-14 06:26:39 【问题描述】:

我正在开发 ios 应用程序。数组上的值存储,但是当我运行应用程序时,它的 崩溃 数组索引超出范围。请提前帮助和感谢..

代码:

daysOfWeek = [NSArray arrayWithObjects:@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday", nil];
    for (int i = 0; i<= [_daysOfWeek count]; i++) 
   _mondayView=[[UIView alloc]initWithFrame:CGRectMake(45, 300  , 830, 30.0)];
         [_mondayView setBackgroundColor:[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:194.0/255.0 alpha:1.0]];
        [_weeklyView addSubview:_mondayView];

        UILabel *daysLabel = [[UILabel alloc]initWithFrame:CGRectMake(65.0f, 300.0f, 120.0f, 30.0f)];
        daysLabel.backgroundColor = [UIColor blueColor];
        daysLabel.text = [NSString stringWithFormat:@"%@",[_daysOfWeek objectAtIndex:i]];
        [_weeklyView addSubview:daysLabel];

    

崩溃日志:

2016-11-14 11:48:41.266 FH Harvey[1839:67640] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 6]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011101934b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000011065d21e objc_exception_throw + 48
    2   CoreFoundation                      0x0000000110f53eeb -[__NSArrayI objectAtIndex:] + 155
    3   FH Harvey                           0x000000010fe1ee62 -[FHWeeklyViewController viewDidLoad] + 1026
    4   UIKit                               0x00000001115ddc99 -[UIViewController loadViewIfRequired] + 1258
    5   UIKit                               0x00000001115e4102 -[UIViewController __viewWillAppear:] + 118
    6   UIKit                               0x000000011160efbf -[UINavigationController _startCustomTransition:] + 1290
    7   UIKit                               0x000000011161fc34 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
    8   UIKit                               0x0000000111620dc7 -[UINavigationController __viewWillLayoutSubviews] + 58
    9   UIKit                               0x0000000111817d6f -[UILayoutContainerView layoutSubviews] + 223
    10  UIKit                               0x0000000111500f50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
    11  QuartzCore                          0x000000010ffbdcc4 -[CALayer layoutSublayers] + 146
    12  QuartzCore                          0x000000010ffb1788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    13  QuartzCore                          0x000000010ffb1606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    14  QuartzCore                          0x000000010ff3f680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
    15  QuartzCore                          0x000000010ff6c767 _ZN2CA11Transaction6commitEv + 475
    16  UIKit                               0x0000000111435a97 _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
    17  UIKit                               0x0000000111c456e0 __handleEventQueue + 5672
    18  CoreFoundation                      0x0000000110fbe311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    19  CoreFoundation                      0x0000000110fa359c __CFRunLoopDoSources0 + 556
    20  CoreFoundation                      0x0000000110fa2a86 __CFRunLoopRun + 918
    21  CoreFoundation                      0x0000000110fa2494 CFRunLoopRunSpecific + 420
    22  GraphicsServices                    0x00000001163a0a6f GSEventRunModal + 161
    23  UIKit                               0x000000011143c964 UIApplicationMain + 159
    24  FH Harvey                           0x000000010fe221bf main + 111
    25  libdyld.dylib                       0x000000011491368d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

【参考方案1】:

问题在于您的 for 循环。数组索引以 0 开头,因此将 for 循环更改为:

for (int i = 0; i < [_daysOfWeek count]; i++) 

您正在使用i&lt;=[_daysOfWeek count],这就是您遇到index 7 beyond bounds [0 .. 6] 崩溃的原因。

您可以使用for each 循环而不是使用for 循环,它易于实现,并且永远不会导致您的索引越界崩溃。

 for (NSString* str in _daysOfWeek) 
     NSLog(@"%@",str);
 

【讨论】:

【参考方案2】:

DEEPAK kumar 只需验证您的逻辑。你的数组有 7 个值,你正在迭代它 8 次。换个逻辑就行了

for (int i = 0; i < [_daysOfWeek count]; i++)

按照 Nirav D 的描述使用for each 循环(最优化的方式)。

编码愉快..

【讨论】:

【参考方案3】:

只需将您的 for 循环逻辑从 i &lt;= [_daysOfWeek count] 更改为 i &lt; [_daysOfWeek count] 即可解决您的问题。但是,为了避免以后出现此类错误,我建议您使用NSArrayenumerateObjectsUsingBlock 方法。可以通过以下方式完成:

NSArray *daysOfWeek = [NSArray arrayWithObjects:@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday", nil];
[daysOfWeek enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) 

];

【讨论】:

【参考方案4】:

请写:

for (int i = 0; i < [_daysOfWeek count]; i++)

它会帮助你。

【讨论】:

以上是关于NSArrayM objectAtIndex:索引 6 超出范围的主要内容,如果未能解决你的问题,请参考以下文章

[__NSArrayM objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'

NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引 5 超出范围'

异常'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 19 超出范围 [0 .. 18]'

-[__NSArrayM objectAtIndex:]: 索引 11 超出界限 [0 .. 10]' 目标 c

由于未捕获的异常“NSRangeException”而终止应用程序。 [__NSArrayM objectAtIndex:]:索引 33 超出范围 [0 .. 32]'

-[__NSArrayM objectAtIndex:]:索引 0 超出了带有 userInfo 的空数组的界限(null)