-2

这是我按下 UITextField 键盘上的完成按钮时得到的结果:(要遵循的代码)

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) 

.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

还有他们:

 @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;
 }
4

2 回答 2

0

崩溃是由于索引越界错误,它发生在您的getWeatherForecast方法中。您可能正在尝试从空数组中获取对象...

于 2013-10-14T22:33:40.613 回答
0
 0x00042a32 -[__NSArrayI objectAtIndex:] + 210

我没有您的 getWeatherForcast 方法的源代码,但我猜该数组是空的。在对数组执行任何代码之前,请尝试:

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

这是安全的,不会因为数组越界而崩溃。(它只是返回零)

我认为 iOS 7 也添加了 firstObject。

于 2013-10-15T03:41:18.663 回答