Nil与预期参数类型不兼容可选<unsafemutablerawpointer>

时间:2016-09-21 08:48:51

标签: swift3

我只是将我的iOS应用程序代码更新为Swift 3,这一行让我感到高兴:

let dataProvider:CGDataProvider? = CGDataProviderCreateWithData(nil, maskImagePixelData!, maskImagePixelData!.count, nil)

在我之前使用的Swift版本中有效,现在它抛出了这个错误:

  

Nil与预期的参数类型不兼容&#39; CGDataProviderReleaseDataCallback&#39; (又名&#39; @convention(c)(可选&lt; UnsafeMutableRawPointer&gt;,UnsafeRawPointer,Int) - &gt; {}&#39;)

我找不到CGDataProviderCreateWithData(!)的文档,但签名也让我感到困惑:

CG_EXTERN CGDataProviderRef __nullable CGDataProviderCreateWithData(
    void * __nullable info, const void * cg_nullable data, size_t size,
    CGDataProviderReleaseDataCallback cg_nullable releaseData)

是否有nil的简单替代品,还是需要进入某种UnsafeMutableRawPointer

1 个答案:

答案 0 :(得分:4)

知道了:

    let releaseMaskImagePixelData: CGDataProviderReleaseDataCallback = { (info: UnsafeMutableRawPointer?, data: UnsafeRawPointer, size: Int) -> () in
        // https://developer.apple.com/reference/coregraphics/cgdataproviderreleasedatacallback
        // N.B. 'CGDataProviderRelease' is unavailable: Core Foundation objects are automatically memory managed
        return
    }
    var dataProvider:CGDataProvider? = CGDataProvider(dataInfo: nil, data: maskImagePixelData!, size: maskImagePixelData!.count, releaseData: releaseMaskImagePixelData)

使用CGDataProvider.init替换CGDataProviderCreateWithData文档为here