Objective C to Swift代码转换,似乎无法在Swift中正确处理字节

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Objective C to Swift代码转换,似乎无法在Swift中正确处理字节相关的知识,希望对你有一定的参考价值。

我正在研究一款简单的Swift蓝牙心率监测器ios App。我找到了this伟大的教程,它有客观的C代码。我已将它转换为Swift,我从心率监测器获取数据。我的问题是我似乎无法正确访问和转换Swift中的字节数据。

这是Objective C代码:

// Instance method to get the heart rate BPM information
- (void) getHeartBPMData:(CBCharacteristic *)characteristic error:(NSError *)error
{
    // Get the Heart Rate Monitor BPM
    NSData *data = [characteristic value];      // 1
    const uint8_t *reportData = [data bytes];
    uint16_t bpm = 0;

    if ((reportData[0] & 0x01) == 0) {          // 2
        // Retrieve the BPM value for the Heart Rate Monitor
        bpm = reportData[1];
    }
    else {
        bpm = CFSwapInt16LittleToHost(*(uint16_t *)(&reportData[1]));  // 3
    }
    // Display the heart rate value to the UI if no error occurred
    if( (characteristic.value)  || !error ) {   // 4
        self.heartRate = bpm;
        self.heartRateBPM.text = [NSString stringWithFormat:@"%i bpm", bpm];
        self.heartRateBPM.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:28];
        [self doHeartBeat];
        self.pulseTimer = [NSTimer scheduledTimerWithTimeInterval:(60. / self.heartRate) target:self selector:@selector(doHeartBeat) userInfo:nil repeats:NO];
    }
    return;
}

这是Swift代码:

func peripheral(peripheral: CBPeripheral!,
    didUpdateValueForCharacteristic characteristic: CBCharacteristic!,
    error: NSError!) -> String
{

    // Get the Heart Rate Monitor BPM
    var data = characteristic.value
    var reportData = data.bytes
    var bpm : UInt16
    var rawByte : UInt8
    var outputString = ""
    rawByte = UInt8(reportData[0])
    bpm = 0

    if ((rawByte & 0x01) == 0) {          // 2
        // Retrieve the BPM value for the Heart Rate Monitor
        bpm = UInt16( reportData[4] )
    }
    else {
        bpm = CFSwapInt16LittleToHost(UInt16(reportData[1]))
    }

    outputString = String(bpm)
    return outputString
}
答案
const uint8_t *reportData = [data bytes];

翻译成

let reportData = UnsafePointer<UInt8>(data.bytes)

然后reportData的类型为UnsafePointer<UInt8>,您可以像(Objective-)C一样访问它:

if (reportData[0] & 0x01) == 0 { ... }

下一个,

bpm = reportData[1];

在Swift中几乎完全相同。你必须明确地从UInt8转换为UInt16,因为 - 与(Objective-)C不同 - Swift不会在类型之间隐式转换:

bpm = UInt16(reportData[1]) 

把它放在一起:

func getHeartBPMData(characteristic: CBCharacteristic!) {
    let data = characteristic.value
    let reportData = UnsafePointer<UInt8>(data.bytes)
    var bpm : UInt16
    if (reportData[0] & 0x01) == 0 {
        bpm = UInt16(reportData[1])
    } else {
        bpm = UnsafePointer<UInt16>(reportData + 1)[0]
        bpm = CFSwapInt16LittleToHost(bpm)
    }

    // ...
}

请注意,您的大多数变量都可以使用let声明为常量,而不是var。代替

bpm = CFSwapInt16LittleToHost(bpm)

您也可以使用适用于所有整数类型的littleEndian:构造函数:

bpm = UInt16(littleEndian: bpm)

Swift 3/4更新:

func getHeartBPMData(characteristic: CBCharacteristic) {
    guard  let reportData = characteristic.value else {
        return 
    }

    let bpm : UInt16
    if (reportData[0] & 0x01) == 0 {
        bpm = UInt16(reportData[1])
    } else {
        bpm = UInt16(littleEndian: reportData.subdata(in: 1..<3).withUnsafeBytes { $0.pointee } )
    }

    // ...
}

以上是关于Objective C to Swift代码转换,似乎无法在Swift中正确处理字节的主要内容,如果未能解决你的问题,请参考以下文章

这应该如何正确地从 Objective C 转换为 swift 3

将代码从 Objective-C 转换为 Swift 后测试用例失败

Swift 3.0 中的 Objective C 代码

将方法从 Objective C 转换为 Swift

将 Objective-C malloc 转换为 Swift

从 Objective C 将回调/闭包转换为 Swift