desperado
desperado

Reputation: 213

Converting Objective-C block to Swift

I have a function written in Objective-C below and I want to convert it to Swift, but I keep getting errors.

Below is the Objective-C code:

- (void)openFileWithFilePathURL:(NSURL*)filePathURL
{
    self.audioFile = [EZAudioFile audioFileWithURL:filePathURL];
    self.filePathLabel.text = filePathURL.lastPathComponent;

    //
    // Plot the whole waveform
    //
    self.audioPlot.plotType = EZPlotTypeBuffer;
    self.audioPlot.shouldFill = YES;
    self.audioPlot.shouldMirror = YES;

    //
    // Get the audio data from the audio file
    //
    __weak typeof (self) weakSelf = self;
    [self.audioFile getWaveformDataWithCompletionBlock:^(float **waveformData,
                                                         int length)
    {
        [weakSelf.audioPlot updateBuffer:waveformData[0]
                          withBufferSize:length];
    }];
}

And here is my Swift code:

func openFileWithFilePathURL(url: NSURL) {
    let audioFile = EZAudioFile(URL: url)
    audioPlot.plotType = EZPlotType.Buffer
    audioPlot.shouldFill = true
    audioPlot.shouldMirror = true


    audioFile.getWaveformDataWithCompletionBlock({(waveformData, length) in
        audioPlot.updateBuffer(waveformData[0], withBufferSize: length)
    })
}

And I always get the error

Command failed due to signal: Segmentation fault: 11

I'm new to iOS language and I spent hours on this problem. I really have no clue on how to fix this problem.

I guess the problem lies in how I converted the block from Objective-C to Swift.

Thank you for your help!!

Upvotes: 2

Views: 199

Answers (2)

JUANM4
JUANM4

Reputation: 344

You can try this:

func openFileWithFilePathURL(filePathURL: NSURL) {
    self.audioFile = EZAudioFile.audioFileWithURL(filePathURL)
    self.filePathLabel.text = filePathURL.lastPathComponent
    //
    // Plot the whole waveform
    //
    self.audioPlot.plotType = EZPlotTypeBuffer
    self.audioPlot.shouldFill = true
    self.audioPlot.shouldMirror = true
    //
    // Get the audio data from the audio file
    //
    weak var weakSelf = self
    self.audioFile.getWaveformDataWithCompletionBlock({(waveformData: Float, length: Int) -> Void in
        weakSelf.audioPlot.updateBuffer(waveformData[0], withBufferSize: length)
    })
}

Upvotes: 1

Mike Schmidt
Mike Schmidt

Reputation: 1065

This is typically an Xcode glitch. The only thing you can do it try to alter the syntax, first by the order of the lines and then the actual lines themselves (i.e. the type of line has several variations). You can also submit a bug report to apple if you can still not fix it. (here)

Upvotes: 1

Related Questions