【发布时间】:2016-10-10 19:39:08
【问题描述】:
我正在尝试将字符串转换为数据类型。我认为这就是我所需要的,但如果我尝试打印它,它只会打印“12 个字节”
let tString = "Hello World!"
if let newData = tString.data(using: String.Encoding.utf8){
print(newData)
self.peripheral?.writeValue(newData, for: positionCharacteristic, type: CBCharacteristicWriteType.withResponse)
}
我做错了什么?
【问题讨论】:
-
没有错。顺便
.utf8就够了。 -
参见例如stackoverflow.com/questions/39075043/…,或只是
print(newData as NSData)。 -
打印 12 个字节的原因是在 UTF8 中存储 12 个字符需要 12 个字节(ASCII 字符每个字符 1 个字节)。
-
@MartinR
as NSData非常聪明。 -
我认为您的代码应该可以正常工作...
标签: swift