2016-08-04 57 views
2

我有一个用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]; 
    }]; 
} 

这里是我的银行代码:

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。

谢谢你的帮助!

+0

https://github.com/syedhali/EZAudio/issues/198 –

+0

http://stackoverflow.com/questions/31305488/how-to- write-block-with-arg-unsafemutablepointerunsafemutablepointerfloat –

+0

根据我的经验,错误(分段错误)是Xcode本身的一个错误,当它在实际有效的语法中窒息时。我通常尝试编译不同的语法变体,直到它工作。 – paulvs

回答

1

你可以试试这个:

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

这通常是一个Xcode故障。你可以做的唯一的事情就是尝试改变语法,首先是线的顺序,然后是实际的线本身(即type of线有几个变化)。如果您仍然无法解决问题,也可以向苹果提交错误报告。 (here