2016-10-10 168 views
5

這是我在以前版本的雨燕的可行代碼:如何將數組轉換爲UnsafeMutablePointer <UnsafeRawPointer?> Swift 3.0?

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) 

後的雨燕3.0的變化我必須將我的鑰匙和值數組到UnsafeMutablePointer<UnsafeRawPointer?>創建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) 

但錯誤泛型參數「實例」無法在行Unmanaged.passUnretained(陣列)推斷出現。 toOpaque()

我不知道如何以編程方式現在創建CFDictionary。

+0

不創建CFDictionary。改爲創建一個Swift Dictionary,例如:http://stackoverflow.com/a/38171414/1187415。 –

+0

我必須用CFDictionary參數調用函數VTCompressionSessionCreate(....)。我不確定我可以只使用Swift Dictionary ...或者它們可以以某種方式轉換嗎? – JULIIncognito

+0

似乎是一種不必要的複雜方式來製作一個'CFDictionary',和一本教科書[XY Problem](http://xyproblem.info)。請參閱http://stackoverflow.com/q/29301302/3141234 – Alexander

回答

相關問題