我正在尝试转换这段源代码:
BluetoothDeviceAddress *deviceAddress = malloc(sizeof(BluetoothDeviceAddress));
转向 Swift,这给了我:
let deviceAddress: BluetoothDeviceAddress = malloc(sizeof(BluetoothDeviceAddress))
但是我发现在Swift 3/4中,不再使用
sizeof
,但这不是我的错误,Xcode返回如下信息:"无法将类型为'UnsafeMutableRawPointer!'的值转换为指定类型'BluetoothDeviceAddress'"
。我尝试改成malloc(MemoryLayout<BluetoothDeviceAddress>.size)
,但仍然出现相同的错误。
编辑:
根据MartinR在评论中提出的建议,我尝试改成let deviceAddress = BluetoothDeviceAddress()
,但是当我想初始化IOBluetoothDevice时仍然会出错(selectedDevice是一个var for IOBluetoothDevice)。self.selectedDevice = IOBluetoothDevice(address: deviceAddress)
错误: 无法将类型为 'BluetoothDeviceAddress' 的值转换为预期的参数类型 'UnsafePointer!'
祝好,
安东尼
let/var deviceAddress = BluetoothDeviceAddress()
? - Martin R