uint8_t*buf=(uint8_t*) [characteristic.value bytes];
_textField.text=[NSString stringWithFormat:@"%d", buf[0]];
I'd like to write this Objective-C code on Swift, but I don't know how to write it well.
I would appreciate it if you could let me know
Write as follows or
var data=characteristic.value
var bytes = [UInt8] (count: data.length, repeatedValue:0)
data.getBytes(&bytes,length:data.length)
textField.text="\(bytes[0])"
Alternatively, the following may be closer to the original Objective-C code.
var data=characteristic.value
letptr = UnsafePointer<UInt8> (characteristic.value.bytes)
let bytes = UnsafeBufferPointer<UInt8> (start:ptr, count:data.length)
textField.text="\(bytes[0])"
© 2025 OneMinuteCode. All rights reserved.