2016-08-04 53 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