2
uint8_t*    buf = (uint8_t*)[characteristic.value bytes];
_textField.text = [NSString stringWithFormat:@"%d", buf[0]];

こちらのObjective-CのコードをSwiftで書きたいのですがうまく書く書き方がわかりません。
教えていただけると嬉しいです

1 件の回答 1

2

次のように書くか

var data = characteristic.value
var bytes = [UInt8](count:data.length, repeatedValue:0)
data.getBytes(&bytes, length:data.length)
textField.text = "\(bytes[0])"

または、下記のほうが元のObjective-Cのコードにより近いかもしれません。

var data = characteristic.value
let ptr = UnsafePointer<UInt8>(characteristic.value.bytes)
let bytes = UnsafeBufferPointer<UInt8>(start:ptr, count:data.length)
textField.text = "\(bytes[0])"
1
  • let buf = UnsafePointer<UInt8>(characteristic.value.bytes) で行けました!ありがとうございます! 2015年7月23日 10:28

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。