一个空数组导致错误

时间:2014-06-17 20:22:09

标签: ios objective-c arrays

它的接缝是一切正常但我得到了一个错误:

  2014-06-17 21:43:49.819 MotionGraphs[1825:907] FFTW-XXXXXXXXX [0]: 512309.971680 , 0.000000 
2014-06-17 21:43:49.831 MotionGraphs[1825:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x319662a3 0x395e697f 0x318b1b75 0xbad69 0x31f18f41 0x321fbd41 0x321f35c1 0x3226bbe3 0x399fe11f 0x399fd4b7 0x399fedcb 0x31939f3b 0x318acebd 0x318acd49 0x3545f2eb 0x337c2301 0xb68f5 0x39a1db20)
libc++abi.dylib: terminate called throwing an exception

我差不多完成了这个应用程序。这是我的代码的一部分(对不起,如果它太长但我担心我会犯一个初学者的错误):

int SIZE =64;
NSMutableArray  *arrayFftwXRe= [NSMutableArray arrayWithCapacity:SIZE];..
NSTimeInterval delta = 0.005;
NSTimeInterval updateInterval = deviceMotionMin + (delta * sliderValue);
NSMutableArray  *sensorData= [NSMutableArray arrayWithCapacity:SIZE]; ...
if ([mManager isDeviceMotionAvailable] == YES) {
    [mManager setDeviceMotionUpdateInterval:updateInterval];
    [mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
        // saving data
        NSString *strSensorData = [NSString stringWithFormat:@"%.6f %.6f %.6f %.6f ", deviceMotion.timestamp, deviceMotion.userAcceleration.x, deviceMotion.userAcceleration.y, deviceMotion.userAcceleration.z];
        NSString *strSensorDataX = [NSString stringWithFormat:@"%.6f %.6f ", deviceMotion.timestamp, deviceMotion.userAcceleration.x];
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex: 0];
        NSString *docFile = [docDir stringByAppendingPathComponent: @"sensordata.txt"];
        NSString *docFileX = [docDir stringByAppendingPathComponent: @"dataFFTWX.txt"];
               if (![fileManager fileExistsAtPath:docDir]){
            [fileManager createFileAtPath:docFile
                                 contents:nil
                               attributes:nil];
            [fileManager createFileAtPath:docFileX
                                 contents:nil
                               attributes:nil];
                    }
        [ sensorData.description writeToFile:docFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
                /////////////////////////////////////// FFTW
                fftw_complex    *data, *fft_result;
        fftw_plan       plan_forward;
        int             i;
        data      = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
        fft_result  = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);

        plan_forward  = fftw_plan_dft_1d(SIZE, data, fft_result,
                                         FFTW_FORWARD, FFTW_ESTIMATE);
        /////////////////////////////////////// Data-X-Y-Z /////////////////
        switch (weakSelf.segmentedControl.selectedSegmentIndex) {
            case kDeviceMotionGraphTypeUserAcceleration:
                [weakSelf setLabelValueX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];
                // userAcceleration
                [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeUserAcceleration] addX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];
                // NSLog(@"Data : %@ ", sensorData  );
                break;
                /////////////////////////////////////// Data-X ////////////////////
            case kDeviceMotionGraphTypeFftwX:
                for( i = 0 ; i < SIZE ; i++ ) {
                    data[i][0] = [sensorDataX [i] floatValue];
                    data[i][1] = 0.0;
                }
                    fftw_execute( plan_forward );
                for( i = 0 ; i < SIZE ; i++ ) {
                    NSLog(@"FFTW-XXXXXXXXX [%d]: %f , %f ",i, fft_result[i][0], fft_result[i][1] ); // working
       fft_result[i][0]=[ arrayFftwXRe[i] floatValue];   // **Here is my ERROR**
                    fft_result[i][1]=[ arrayFftwXIm[i] floatValue];    // **Here is my ERROR TOO**
                    ////////////////show Graph
                    [ arrayFftwXRe.description writeToFile:docFileX atomically:YES encoding:NSUTF8StringEncoding error:nil];
                    [weakSelf setLabelValueX:fft_result[i][0] y:fft_result[i][1] z:deviceMotion.userAcceleration.z];
                    [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeFftwX] addX:fft_result[i][0] y: fft_result[i][1] z:4];
                }
                break;

每一次帮助都会很棒。我应该更改此代码中的内容(以使其更好)吗?

1 个答案:

答案 0 :(得分:1)

无论arrayFftwXRe是什么,都是空的。您的代码位于:

fft_result[i][0]=[ arrayFftwXRe[i] floatValue];

要求arrayFftwXRe的索引0,arrayFftwXRe为空/未初始化。您需要查找与其相关的代码部分。

评论中的代码:

上面的代码行:

  1. arrayFftwXRe[i] //由于数组为空或未初始化而失败
  2. 从第1步获取值(已经崩溃了)并获取其floatValue
  3. 将步骤2中的值分配到fft_result[i][0](但很久以前就崩溃了