【发布时间】:2016-07-02 09:13:07
【问题描述】:
我有一个问题。我正在尝试与 Mi Band 交互。在 github 上找到了这段代码,它运行良好。但是我不明白数据类型转换发生了什么。
var u16 = UnsafePointer<Int32>(characteristic.value!.bytes).memory
来自这个代码块:
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
output("Data for "+characteristic.UUID.UUIDString, data: characteristic.value!)
if(characteristic.UUID.UUIDString == "FF06") {
spinnerView.hidden = true
let u16 = UnsafePointer<Int>(characteristic.value!.bytes).memory
stepsView.stringValue = ("\(u16) steps")
} else if(characteristic.UUID.UUIDString == "FF0C") {
spinnerView.hidden = true
var u16 = UnsafePointer<Int32>(characteristic.value!.bytes).memory
u16 = u16 & 0xff
batteryView.stringValue = ("\(u16) % charged")
}
}
谁能给我解释一下?谢谢!
【问题讨论】:
-
这是一种将 NSData 转换为其他值(在我们的例子中,它们是
int或Int32)的方法(或最新版本的 Swift 中的新方法,我不知道)。之前是getBytes:length:。 -
@Larme 感谢您的回复。在那种情况下,为什么要使用“UnsafePointer”?
标签: ios swift macos bluetooth-lowenergy core-bluetooth