将错误 OBJ-C 转换为 Swift 3

Conversion error OBJ-C to Swift 3

我正在尝试将 EZAudio (https://github.com/syedhali/EZAudio) Objective-C 库转换为 Swift 3. 我正在执行其中的 "EZAudioPlayFileExample" 部分。

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 3

func openFileWithFilePathURL(filePathURL: NSURL) {
        self.audioFile = EZAudioFile(url: filePathURL as URL!)

        //
        // Plot the whole waveform
        //
        self.plot.plotType = .buffer
        self.plot.shouldFill = true
        self.plot.shouldMirror = true

        //
        // Get the audio data from the audio file
        //
        weak var weakSelf = self
        self.audioFile.getWaveformData(completionBlock: {(_ waveformData: Float, _ length: Int) -> Void in
            weakSelf?.plot.updateBuffer(waveformData[0], withBufferSize: length) // error in this line as Float as no subscript members
        })
    }

我知道 Float 参数没有像 [0] 这样的下标成员。但是我想转换 Objective-C 代码。或者这里的任何人都使用这个库的 Swift 版本。注意:我想要 "EZAudioPlayFileExample" 在里面。

float **waveformData实际上是一个unsafemutablepointer。所以你必须在 swift 3.0 中使用 unsafemutablepointer。 您可以在 swift 中使用 unsafemutablepointer,例如

UnsafeMutablePointer<UnsafeMutablePointer<Float>>

你可以使用 like

self.audioFile.getWaveformData(completionBlock: {(_ waveformData: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, _ length: Int) -> Void in
            weakSelf?.plot.updateBuffer(waveformData[0], withBufferSize: length)  
        })

我刚刚下载了 EZAudio 的旧版本 swift 实现并转换为 swift 3.0。可以在这个 link
我不知道这对您有何帮助,但您可以在此基础上进行构建。 您还可以参考 gitHub 页面

上报告的 issue