因未捕获的异常'NSRangeException'xcode 5而终止应用程序

时间:2013-11-21 09:24:06

标签: ios iphone xcode ipad

我在选择器视图函数中遇到此错误,该函数将其数据从google电子表格中获取为文本文件..这是函数的代码。

{
- (IBAction)clickText:(id)sender

int tag = [(UITextField*)sender tag];
if (tag!=11)
    if (tag!=12)
        if (tag!=13)
            if (tag!=14)
                if (tag!=15)
                    if (tag!=16)
                        if (tag!=17)
                            if (tag!=18)
                                if (tag!=19)


                                        {
                                            [sender resignFirstResponder];
                                        }
self.myPicker.hidden = NO;
selectedTable = tag;
[sender resignFirstResponder];
float yy = 10;
switch (tag) {
    case 11: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
        break;
    case 12: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
        break;
    case 13: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
        break;
    case 14: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 15: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 16: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 17: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 18: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
        break;
    case 19: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
        break;


    default:
        break;
}

[self.myPicker setFrame:CGRectMake(self.txtTgfeet.frame.origin.x, yy, self.txtTgfeet.frame.size.width, 216)];

[self.ar removeAllObjects];

NSString *theURL = @"https://docs.google.com/spreadsheet/pub?key=0AobIgii9hBGPdDJ3TmgxSkRzVFBKWXVnTWdQcGtyMFE&single=true&gid=9&output=txt";
NSString *theFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:theURL] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",theFile);
NSArray *theCells = [theFile componentsSeparatedByString:@"\n"];



for (int i=1; i < [theCells count]; i++)
{
    //        if (i > 0)
    {
        NSArray *value =[[theCells objectAtIndex:i] componentsSeparatedByString:@"\t"];
        [self.ar addObject:[value objectAtIndex:tag+1]];

    }
}
[self.myPicker selectRow:0 inComponent:0 animated:NO];
[self.myPicker reloadAllComponents];

此功能在其他应用程序上完美运行,我已经为另一个我想要此功能的应用程序做了这个。现在,当我单击文本字段时,我收到此错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 12 beyond bounds [0 .. 8]'
*** First throw call stack:
(
0   CoreFoundation                      0x01b575e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x018da8b6 objc_exception_throw + 44
2   CoreFoundation                      0x01af84e6 -[__NSArrayM objectAtIndex:] + 246
3   TGC                                 0x000080ba -[MainViewController clickText:] + 4602
4   libobjc.A.dylib                     0x018ec81f -[NSObject performSelector:withObject:] + 70
5   UIKit                               0x0043d0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
6   UIKit                               0x0043d04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
7   UIKit                               0x005350c1 -[UIControl sendAction:to:forEvent:] + 66
8   UIKit                               0x00535484 -[UIControl _sendActionsForEvents:withEvent:] + 577
9   UIKit                               0x00b222ae -[UITextField willAttachFieldEditor:] + 685
10  UIKit                               0x0053aeb5 -[UIFieldEditor becomeFieldEditorForView:] + 927
11  UIKit                               0x00b193a3 -[UITextField _becomeFirstResponder] + 160
12  UIKit                               0x00594ef5 -[UIResponder becomeFirstResponder] + 400
13  UIKit                               0x0049819b -[UIView(Hierarchy) becomeFirstResponder] + 114
14  UIKit                               0x00b18e43 -[UITextField becomeFirstResponder] + 51
15  UIKit                               0x007bd551 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 135
16  UIKit                               0x007bfaa2 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 2640
17  UIKit                               0x007b3e8c _UIGestureRecognizerSendActions + 230
18  UIKit                               0x007b2b00 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
19  UIKit                               0x007b456d -[UIGestureRecognizer _delayedUpdateGesture] + 60
20  UIKit                               0x007b7acd ___UIGestureRecognizerUpdate_block_invoke + 57
21  UIKit                               0x007b7a4e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
22  UIKit                               0x007ae148 _UIGestureRecognizerUpdate + 199
23  UIKit                               0x0047a19a -[UIWindow _sendGesturesForEvent:] + 1291
24  UIKit                               0x0047b0ba -[UIWindow sendEvent:] + 1030
25  UIKit                               0x0044ee86 -[UIApplication sendEvent:] + 242
26  UIKit                               0x0043918f _UIApplicationHandleEventQueue + 11421
27  CoreFoundation                      0x01ae083f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
28  CoreFoundation                      0x01ae01cb __CFRunLoopDoSources0 + 235
29  CoreFoundation                      0x01afd29e __CFRunLoopRun + 910
30  CoreFoundation                      0x01afcac3 CFRunLoopRunSpecific + 467
31  CoreFoundation                      0x01afc8db CFRunLoopRunInMode + 123
32  GraphicsServices                    0x031bb9e2 GSEventRunModal + 192
33  GraphicsServices                    0x031bb809 GSEventRun + 104
34  UIKit                               0x0043bd3b UIApplicationMain + 1225
35  TGC                                 0x00002d62 main + 130
36  libdyld.dylib                       0x022fd725 start + 0
37  ???                                 0x00000001 0x0 + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException

这里的问题似乎是什么?

3 个答案:

答案 0 :(得分:1)

这里明确提到[__NSArrayM objectAtIndex:]:索引12超出界限[0 .. 8]'

您正在尝试访问第12个[索引12且数组只有0到8个对象]元素,因此应用程序崩溃。

使用下面的断点检查

 [self.ar addObject:[value objectAtIndex:tag+1]];

答案 1 :(得分:0)

你的问题似乎在这里:

[value objectAtIndex:tag+1]

当您的数组value仅包含9个元素时,您正在访问索引12。

答案 2 :(得分:0)

NSArray *value =[[theCells objectAtIndex:i] componentsSeparatedByString:@"\t"];
 if ([value count]>tag+1) {
    [self.ar addObject:[value objectAtIndex:tag+1]];
    }