Xcode 10.1无法调用'copyBytes'

时间:2019-07-19 20:24:13

标签: ios swift xcode byte

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

1 个答案:

答案 0 :(得分:2)

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)
    })
}