gpt4 book ai didi

objective-c - 将 Objective-C 代码转换为 Swift

转载 作者:行者123 更新时间:2023-11-28 08:54:07 24 4
gpt4 key购买 nike

我一直在使用 nrf51x。我有一个用 Obj-C 编写的示例代码。我无法将其转换为 Swift。

uint8_t value = ACTIVATE_AND_RESET_REQUEST;
[self.bluetoothPeripheral writeValue:[NSData dataWithBytes:&value length:sizeof(value)] forCharacteristic:self.dfuControlPointCharacteristic type:CBCharacteristicWriteWithResponse];

我试过了

var value: UInt8 = DfuOperations.VALIDATE_FIRMWARE_REQUEST.rawValue
let ptr = UnsafePointer<Void>(value)
let data = NSData(ptr, length: sizeofValue(value))
dfuPeripheral!.writeValue(data, forCharacteristic: self.dfuControlPointCharacteristic, type: CBCharacteristicWriteType.WithResponse)

还有这个

var value: UInt8 = DfuOperations.VALIDATE_FIRMWARE_REQUEST.rawValue
let data = NSData(&value, length: sizeofValue(value))

谁能帮帮我?非常感谢

最佳答案

下面的类构建没有错误。我正在使用 Xcode 7.1。

import Foundation
import CoreBluetooth

class Test {
func test(bluetoothPeripheral: CBPeripheral, dfuControlPointCharacteristic: CBCharacteristic) {
let value = UInt8(ACTIVATE_AND_RESET_REQUEST.rawValue)
let encodedValue = NSData(bytes: [value], length: sizeof(UInt8))
bluetoothPeripheral.writeValue(encodedValue, forCharacteristic: dfuControlPointCharacteristic, type: CBCharacteristicWriteType.WithResponse)
}
}

关于objective-c - 将 Objective-C 代码转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com