使用Xcode 10.1时出现此错误:
无法使用类型为'(to:(UnsafeMutableRawBufferPointer),from:ClosedRange)'的参数列表调用'copyBytes'
在此代码行上:
_ = withUnsafeMutableBytes(of: &humid) {characteristic.value!.copyBytes(to: $0, from: 6...7)}
但是它可以在Xcode 10.2中构建并正常运行。问题是我们的构建服务器使用Xcode 10.1,在这里我有什么选择?
这是上下文代码:
var humid: UInt16 = 0
//_ = withUnsafeMutableBytes(of: &humid) {characteristic.value!.copyBytes(to: $0, from: 6...7)}
_ = withUnsafeMutablePointer(to: &humid, {
_ = data.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1), from: 6..<7)
})
humid = humid / 100
weatherReading.humidity = Double(humid)
答案 0 :(得分:2)
copyBytes()
带有一个UnsafeMutableBufferPointer
参数。示例:
func peripheral(_ peripheral: CBPeripheral,
didUpdateValueFor characteristic: CBCharacteristic,
error: Error?) {
let data = characteristic.value!
var humid: UInt16 = 0
_ = withUnsafeMutablePointer(to: &humid, {
_ = data.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1),
from: 6..<8)
})
}