【问题标题】:How to convert array to UnsafeMutablePointer<UnsafeRawPointer?> Swift 3.0?如何将数组转换为 UnsafeMutablePointer<UnsafeRawPointer?> Swift 3.0?
【发布时间】:2017-02-19 07:40:39
【问题描述】:

这是我在以前版本的 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)

在 Swift 3.0 中发生更改后,我必须将我的键和值数组转换为 UnsafeMutablePointer&lt;UnsafeRawPointer?&gt; 以创建 CFDictionary。

这边:

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

给出错误访问错误。

阅读文档后,我正在尝试编译此代码:

        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)

但错误Generic parameter 'Instance' could not be inferred出现在Unmanaged.passUnretained(array).toOpaque()

我现在不知道如何以编程方式创建 CFDictionary。

【问题讨论】:

  • 不要创建 CFDictionary。而是创建一个 Swift 字典,例如这里:stackoverflow.com/a/38171414/1187415.
  • 我必须使用 CFDictionary 参数调用函数 VTCompressionSessionCreate(....)。我不确定我是否可以只使用 Swift 字典......或者它们是否可以以某种方式转换?
  • 似乎是一种不必要的复杂方法来制作CFDictionary 和教科书XY Problem。见stackoverflow.com/q/29301302/3141234
  • 现在我只是简化了 CFDictionary 的创建。看起来字典已创建,但我的下一步仍然无法正常工作。可能是因为这本词典,我不确定stackoverflow.com/questions/39987373/…

标签: ios swift swift3 unsafe-pointers unsafemutablepointer


【解决方案1】:

我刚刚解决了将数组转换为UnsafeMutablePointer&lt; UnsafeMutablePointer&lt;T&gt;&gt; 的类似问题,您可以在此处找到: Swift 3 UnsafeMutablePointer initialization for C type float**

要使用相同的方案转换 swift 数组,请按照此处的建议使用 UnsafeMuTablePointerhttp://technology.meronapps.com/2016/09/27/swift-3-0-unsafe-world-2/

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 2019-12-08
    • 2021-06-29
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多