我正在尝试从正在使用的蓝牙设备转换值中的字节,但是我在此上得到了Generic parameter 'Self' could not be inferred
,我不知道这是什么意思
var speed: UInt16 = 0
_ = withUnsafeBytes(of: &speed, {characteristic.value!.copyBytes(to: $0, from: 0...1)})
答案 0 :(得分:0)
该错误消息具有误导性。实际的问题是withUnsafeBytes
用指向给定参数原始字节的只读缓冲区指针调用闭包。您需要的是withUnsafeMutableBytes
:
var speed: UInt16 = 0
_ = withUnsafeMutableBytes(of: &speed, {
characteristic.value!.copyBytes(to: $0, from: 0...1)
})
答案 1 :(得分:0)
需要更改为
_ = withUnsafeMutableBytes(of: &speed, {
characteristic.value!.copyBytes(to: UnsafeMutuableBufferPointer(to: $0. count: 1), from: 0..<1)
})