Question

This is what I get when I press the done button on the keyboard of a UITextField: (code to follow)

2013-10-14 23:22:55.170 What to where?[21627:a0b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0008e5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0178e8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x00042a32 -[__NSArrayI objectAtIndex:] + 210
    3   What to where?                      0x00002670 -[ViewController getWeatherForecast] + 352
    4   What to where?                      0x00002430 -[ViewController textFieldDidEndEditing:] + 288
    5   libobjc.A.dylib                     0x017a081f -[NSObject performSelector:withObject:] + 70
    6   UIKit                               0x00502c8c -[UIApplication sendAction:to:from:forEvent:] + 108
    7   UIKit                               0x00502c18 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    8   UIKit                               0x005fa6d9 -[UIControl sendAction:to:forEvent:] + 66
    9   UIKit                               0x005faa9c -[UIControl _sendActionsForEvents:withEvent:] + 577
    10  UIKit                               0x00bddaf3 -[UITextField _resignFirstResponder] + 300
    11  UIKit                               0x0065a71e -[UIResponder _finishResignFirstResponder] + 181
    12  UIKit                               0x0065a82f -[UIResponder resignFirstResponder] + 265
    13  UIKit                               0x00bdd894 -[UITextField resignFirstResponder] + 118
    14  What to where?                      0x000024d9 -[ViewController textFieldShouldReturn:] + 137
    15  UIKit                               0x00be68bf -[UITextField keyboardInput:shouldInsertText:isMarkedText:] + 357
    16  UIKit                               0x006d5a43 -[UIKeyboardImpl callShouldInsertText:] + 271
    17  UIKit                               0x006dfd20 -[UIKeyboardImpl addWordTerminator:afterSpace:elapsedTime:executionContext:] + 63
    18  UIKit                               0x006df81a -[UIKeyboardImpl completeAcceptCandidateBeforeAddingInput:executionContext:] + 1668
    19  UIKit                               0x006df0f6 __60-[UIKeyboardImpl addInputString:withFlags:executionContext:]_block_invoke + 53
    20  UIKit                               0x00c0dc6e -[UIKeyboardTaskExecutionContext returnExecutionToParent] + 254
    21  UIKit                               0x006e299b -[UIKeyboardImpl acceptCurrentCandidateIfSelectedWithExecutionContext:] + 250
    22  UIKit                               0x006df0b9 -[UIKeyboardImpl addInputString:withFlags:executionContext:] + 451
    23  UIKit                               0x006ddf7f -[UIKeyboardImpl handleStringInput:withFlags:executionContext:] + 248
    24  UIKit                               0x006ec16f -[UIKeyboardImpl handleKeyWithString:forKeyEvent:executionContext:] + 633
    25  UIKit                               0x006ebcb6 -[UIKeyboardImpl handleKeyEvent:executionContext:] + 1808
    26  UIKit                               0x006eb50f __33-[UIKeyboardImpl handleKeyEvent:]_block_invoke + 51
    27  UIKit                               0x006eb31c -[UIKeyboardImpl _handleKeyEvent:executionContext:] + 243
    28  UIKit                               0x008642e9 -[UIKeyboardLayoutStar completeRetestForTouchUp:timestamp:interval:executionContext:] + 4078
    29  UIKit                               0x00862def -[UIKeyboardLayoutStar touchUp:executionContext:] + 1545
    30  UIKit                               0x006f9398 __28-[UIKeyboardLayout touchUp:]_block_invoke + 269
    31  UIKit                               0x00c0e1b8 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
    32  UIKit                               0x00c0e85f -[UIKeyboardTaskQueue addTask:] + 144
    33  UIKit                               0x006f9283 -[UIKeyboardLayout touchUp:] + 263
    34  UIKit                               0x006f9db9 -[UIKeyboardLayout touchesEnded:withEvent:] + 362
    35  UIKit                               0x005400cd -[UIWindow _sendTouchesForEvent:] + 852
    36  UIKit                               0x00540d34 -[UIWindow sendEvent:] + 1232
    37  UIKit                               0x00514a36 -[UIApplication sendEvent:] + 242
    38  UIKit                               0x004fed9f _UIApplicationHandleEventQueue + 11421
    39  CoreFoundation                      0x000178af __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    40  CoreFoundation                      0x0001723b __CFRunLoopDoSources0 + 235
    41  CoreFoundation                      0x0003430e __CFRunLoopRun + 910
    42  CoreFoundation                      0x00033b33 CFRunLoopRunSpecific + 467
    43  CoreFoundation                      0x0003394b CFRunLoopRunInMode + 123
    44  GraphicsServices                    0x036859d7 GSEventRunModal + 192
    45  GraphicsServices                    0x036857fe GSEventRun + 104
    46  UIKit                               0x0050194b UIApplicationMain + 1225
    47  What to where?                      0x00002d3d main + 141
    48  libdyld.dylib                       0x01fbf725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

The code in the .h :

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface ViewController : UIViewController <UITextFieldDelegate> {

    NSString *location;
    NSString *time;

 }

 @property (strong, nonatomic) IBOutlet UITextField *destinationTF;
 @property (strong, nonatomic) IBOutlet UITextField *timeTF;

 - (IBAction)textFieldDidEndEditing:(id)sender;

 @end

And the .m:

 @synthesize destinationTF, timeTF;

 - (void)viewDidLoad
 {
     [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.

 }

 - (IBAction)textFieldDidEndEditing:(id)sender {
     if (sender == destinationTF ) {

         location = destinationTF.text;

     } else if (sender == timeTF) {

         time = timeTF.text;
     }

     [self getWeatherForecast];
 }

 - (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [destinationTF resignFirstResponder];
    [timeTF resignFirstResponder];

    return YES;
 }
Was it helpful?

Solution

 0x00042a32 -[__NSArrayI objectAtIndex:] + 210

I don't have the source code to your getWeatherForcast method, but I would guess that that array is empty. Before you execute any code on the array try:

if ([someArray lastObject]) {
    ..Do something
} else {
    do something if the test fails
}

This is safe and will not crash because the array is out of bounds. (It just returns nil)

I think iOS 7 added firstObject as well.

OTHER TIPS

The crash is due to an index out of bounds error and it is occurring in your getWeatherForecast method. You may be attempting to get an object out of an empty array...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top