I don't know how to cast uint8_t on Swift.

Asked 2 years ago, Updated 2 years ago, 76 views

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

objective-c swift bluetooth

2022-09-30 19:46

1 Answers

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])"


2022-09-30 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.