我想轉換此源代碼:不能UnsafeMutableRawPointer的值轉換爲BluetoothDeviceAddress
BluetoothDeviceAddress *deviceAddress = malloc(sizeof(BluetoothDeviceAddress));
斯威夫特,這給了我:
let deviceAddress: BluetoothDeviceAddress = malloc(sizeof(BluetoothDeviceAddress))
但是,我發現,在斯威夫特3/4,sizeof
不再使用,但這不是我的錯誤,Xcode返回:
「無法轉換'UnsafeMutableRawPointer!'類型的值到指定類型「BluetoothDeviceAddress」「
我試圖更改爲malloc(MemoryLayout<BluetoothDeviceAddress>.size)
但仍然是相同的錯誤。
編輯: 作爲由MartinR的意見建議,我試圖改變到let deviceAddress = BluetoothDeviceAddress()
但後來當我想初始化IOBluetoothDevice,我還得到一個錯誤(selectedDevice是IOBluetoothDevice一個VAR):
self.selectedDevice = IOBluetoothDevice(address: deviceAddress)
錯誤:無法將類型'BluetoothDeviceAddress'的值轉換爲期望的參數類型'UnsafePointer!'
最佳,
安東尼
爲什麼你必須*分配內存?爲什麼不只是'讓/ var deviceAddress = BluetoothDeviceAddress()'? –
@MartinR無法正常工作,請參閱我的編輯 – Antoine