[英]C interoperability with Swift
我在Swift 2.2 iOS項目中包含了一個C結構。 C結構通過頭文件公開,如下所示:
#define NUM_BINS 10;
typedef struct
{
float bin_size;
unsigned short counts[NUM_BINS];
unsigned int cleanSamples;
unsigned short compressCount;
unsigned short totalSamples;
bool isMature;
} SD;
然后我嘗試通過以下方式在Swift中聲明此結構:
var counts: [UInt16] = [1,2,3,4,5,6,7,8,9,10]
let sd = SD(bin_size: 500, counts: counts, cleanSamples: 0, compressCount: 0, totalSamples: 0, isMature: false)
但收到以下錯誤:
無法將'[UInt16]'類型的值轉換為預期的參數類型'(UInt16,UInt16,UInt16,UInt16,UInt16,UInt16,UInt16,UInt16,UInt16,UInt16)'
我怎樣才能將這個從C轉換為Swift?
謝謝!
固定大小的C數組變成Swift元組而不是Swift數組。 (見這里有一些不幸的后果。)所以而不是
var counts: [UInt16] = [1,2,3,4,5,6,7,8,9,10]
你需要類似的東西
var counts = (1,2,3,4,5,6,7,8,9,10)
順便說一下,我在Apple的官方文檔中沒有找到任何說法。 我不知道是否有理由擔心它將來會發生變化......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.