我试图将字符串转换为数据类型。我认为这是我所需要的,但如果我尝试打印它只是打印出“12个字节”将字符串转换为swift中的数据3.0
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'就足够了。 – vadian
参见http://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift,或者只是'print(newData as NSData)'。 –
打印12字节的原因是需要12个字节来存储UTF8中的12个字符(ASCII字符每个字符1个字节)。 –