当使用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)

最佳答案

Swift 4.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)
    })
}

07-24 13:29