将Objective-C块转换为Swift

时间:2016-08-04 12:33:34

标签: ios objective-c swift

我有一个用Objective-C编写的函数,我希望将它转换为Swift,但我一直都会遇到错误。

以下是Objective-C代码:

- (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];
    }];
}

这是我的Swift代码:

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)
    })
}

我总是得到错误

Command failed due to signal: Segmentation fault: 11

我是iOS语言的新手,我在这个问题上花了好几个小时。我真的不知道如何解决这个问题。

我想问题在于我如何将块从Objective-C转换为Swift。

感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

这通常是Xcode故障。你可以做的唯一的事情是尝试改变语法,首先是行的顺序,然后是实际的行本身(即google.maps.event.addListener(markerA, 'dragend', function () { var newPointA = markerA.getPosition(); var newPointB = markerB.getPosition(); console.log("point1"+ newPointA); console.log("point2"+ newPointB); }); google.maps.event.addListener(markerB, 'dragend', function () { var newPointA = markerA.getPosition(); var newPointB = markerB.getPosition(); console.log("point1"+ newPointA); console.log("point2"+ newPointB); }); 行有几个变化)。如果仍然无法解决问题,您也可以向苹果提交错误报告。 (here

答案 1 :(得分:1)

你可以试试这个:

b = (a.getName = a.getName.bind(a))();
console.log(b, window); // manish Window