无法推断通用参数'self'



我正在尝试从我正在使用的蓝牙设备转换值中的字节,但我对此感到Generic parameter 'Self' could not be inferred,我不知道这意味着什么

var speed: UInt16 = 0
_ = withUnsafeBytes(of: &speed, {characteristic.value!.copyBytes(to: $0, from: 0...1)})

错误消息具有误导性。实际问题是withUnsafeBytes使用指向给定参数的原始字节的只读缓冲区指针调用闭包。您需要的是withUnsafeMutableBytes

var speed: UInt16 = 0
_ = withUnsafeMutableBytes(of: &speed, {
    characteristic.value!.copyBytes(to: $0, from: 0...1)
})

需要更改为

_ = withUnsafeMutableBytes(of: &speed, {
    characteristic.value!.copyBytes(to: UnsafeMutuableBufferPointer(to: $0. count: 1), from: 0..<1)
})

最新更新