它似乎可以正常工作,但随后出现错误:

  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;


每一次帮助都会很棒。我是否应该更改此代码中的某些内容(使其变得更好)?

最佳答案

arrayFftwXRe都是空的。您的代码位于:

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


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

来自评论的代码:

上面的代码行执行以下操作:


arrayFftwXRe[i] //此操作失败,因为数组为空或未初始化
从第1步中获取值(已经崩溃,记住)并获取其floatValue
将第2步中的值分配给fft_result[i][0](但是您很久以前就崩溃了

09-11 13:29