How to convert array to UnsafeMutablePointer<UnsafeRawPointer?> Swift 3.0?

女生的网名这么多〃 提交于 2019-12-08 21:58:20

问题


Here was my workable code in the previous version of Swift:

 let imageOptionsDictKeys = [ kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,  kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey, kCVPixelBufferIOSurfacePropertiesKey]
 let imageOptionsDictValues = [ cvPixelFormatType,  frameW, frameH, boolYES]

 var keyCallbacks = kCFTypeDictionaryKeyCallBacks
 var valueCallbacks = kCFTypeDictionaryValueCallBacks

 let imageOptions = CFDictionaryCreate(kCFAllocatorDefault, UnsafeMutablePointer(imageOptionsDictKeys), UnsafeMutablePointer(imageOptionsDictValues), 4, &keyCallbacks, &valueCallbacks)

After changes in the Swift 3.0 I have to convert my keys and values arrays into UnsafeMutablePointer<UnsafeRawPointer?> for creating CFDictionary.

This way:

    let imageOptionsDictKeysPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
    imageOptionsDictKeysPointer.initialize(to: imageOptionsDictKeys)

gives an Bad Access error.

And after reading documentation I am trying to compile this code:

        let imageOptionsDictKeys = [kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey,  kCVPixelBufferHeightKey, kCVPixelBufferOpenGLESCompatibilityKey]
        let imageOptionsDictKeysRawPointer = Unmanaged.passUnretained(imageOptionsDictKeys).toOpaque()
        let imageOptionsDictKeysPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
        imageOptionsDictKeysPointer.initialize(to: imageOptionsDictKeysRawPointer)

        let imageOptionsDictValues = [ cvPixelFormatType,  frameW, frameH, boolYES]
        let imageOptionsDictValuesRawPointer = Unmanaged.passUnretained(imageOptionsDictValues).toOpaque()
        let imageOptionsDictValuesPointer =  UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 1)
        imageOptionsDictValuesPointer.initialize(to: imageOptionsDictValuesRawPointer)

        let imageOptions = CFDictionaryCreate(kCFAllocatorDefault, imageOptionsDictKeysPointer, imageOptionsDictValuesPointer, 4, &keyCallbacks, &valueCallbacks)

but error Generic parameter 'Instance' could not be inferred appears in the lines Unmanaged.passUnretained(array).toOpaque()

I have no idea how to create CFDictionary programmatically now.


回答1:


I just solved a similar issue converting arrays to UnsafeMutablePointer< UnsafeMutablePointer<T>> which you can find here: Swift 3 UnsafeMutablePointer initialization for C type float**

To convert swift arrays using the same scheme, use UnsafeMuTablePointer as suggested here: http://technology.meronapps.com/2016/09/27/swift-3-0-unsafe-world-2/



来源:https://stackoverflow.com/questions/39965810/how-to-convert-array-to-unsafemutablepointerunsaferawpointer-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!