2015-07-22 158 views
2

我有一個C函數需​​要一個指針。 我想從Swift中調用它。將inout值轉換爲UnsafeMutablePointer <非託管<TYPE>?>

Calling C function from Swift(和個人經驗),做& var不起作用。

不過,我不能夠施展&變種成UnsafeMutablePointer ......也沒有任何類型的轉換(如VAR分配的地址類型UnsafeMutablePointer的另一個變量)

(假設類型是一個對象)

感謝

示例代碼:

var zero = CreateCompressionSession(&session as UnsafeMutablePointer<Unmanaged<VTCompressionSession>?>, Int32(CVPixelBufferGetWidth(pixelBuffer)), Int32(CVPixelBufferGetHeight(pixelBuffer))) 

而試圖讓它撥打:

int CreateCompressionSession(VTCompressionSessionRef* session, int width, int height) { 
    OSStatus err = VTCompressionSessionCreate(NULL, width, height, kCMVideoCodecType_H264, NULL, NULL, NULL, (VTCompressionOutputCallback)vtCallback, NULL, session); 
    NSLog(@"%d %p", (int)err, session); 
    if (err == noErr) { 
     return 0; 
    } else { 
     return err; 
    } 
} 
+0

您是否檢查過Apple的指南,以便從Swift使用C? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html和https://developer.apple.com/swift/blog/?id=6 –

+1

是的,不確定是否只是我,但他們使用&VAR,它的工作(雖然他們迅速執行快速功能?有點奇怪...)。但我得到錯誤。我沒有閱讀這些文檔的內容嗎? – dcheng

回答

0

也許你遇到的問題的行爲是由於舊版本的雨燕,但我的經驗& var目錄應該實際工作。

通過橋頭中包含的C頭,確保CreateCompressionSession()和VTCompressionSessionRef(以及它指向的指針類型)可用於Swift代碼。查看Swift簽名是如何導入到Swift中的,以及如何導入VTCompressionSessionRef的CreateCompressionSession()。根據導入的結構處理Swift代碼。如果你願意的話,你可以在他們身邊編寫便利包裝。

一些額外的信息,請參閱這個問題:Swift converts C's uint64_t different than it uses its own UInt64 type

這裏也是一個文章,這可能有助於:http://www.swiftprogrammer.info/callback_void.html

很抱歉,如果這是不是非常有幫助,因爲我不熟悉的所有細節你正在嘗試做什麼,但希望這會有所幫助。

相關問題