2013-11-21 45 views
0

終止應用程序我在一個選擇器視圖函數中得到了這個錯誤,它將數據從google電子表格中的文本文件..這是該函數的代碼。由於未捕獲的異常'NSRangeException'xcode 5

{ 
- (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 

什麼似乎是這裏的問題?

+2

爲什麼使用標籤來識別觀點的一個很好的例子是非常脆。對象引用在可用時更好。一個'if'語句將代碼推到最右邊,一個大的'switch'語句是代碼味道。 – Abizern

+1

if(!(tag> = 11)&&!(tag <= 19)) –

+0

是的,我已經修好了,謝謝! – user2818570

回答

1

在這裏可以清楚地提到,[__NSArrayM objectAtIndex:]:索引12超出範圍[0 .. 8]」

您正在嘗試訪問第12 [指數12和陣列具有0-8對象只]元素這是不存在的,所以應用程序崩潰。

使用以下行斷點檢查,

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

你的問題似乎是在這裏:

[value objectAtIndex:tag+1] 

您正在訪問的指數12,當你的陣列value只包含9個元素。

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