swift - 将 UUID 转换为字符串表示,例如:1816 转换为 Cycling Speed 和 Cadence
问题描述
我正在通过蓝牙连接到 Cycling Speed and Cadence 服务,并将外围设备保存到数组中(用于 tableview)。
我已经实现了这个结构
struct BTPeripheral: Identifiable {
let id: Int
let name: String
let uuid: UUID
let desc: String
}
我打电话的是
discoveredBTDevices.append(BTPeripheral.init(
id: discoveredBTDevices.count,
name: peripheral.name!,
uuid: peripheral.identifier,
desc: service.uuid.uuidstring) // trying service.uuid results in error
)
这导致了这个输出
id: 0,
name: "Wahoo CADENCE 1DE8",
uuid: E98F3843-1C6A-E4DE-6EF3-76B013950007,
desc: "1816"
描述为字符串 1816(即循环 Speed & Cad 服务的 UUID)
let serviceCyclingSpeedAndCadenceUUID = CBUUID(string: "1816")
如何将 1816 转换为打印出来的字符串“Cycling Speed and Cadence”
print(service.uuid) // this results in "cycling Speed and Cadence"
如上所述,放置service.uuid
会导致错误Cannot convert value of type 'CBUUID' to expected argument type 'String'
解决方案
你可以得到它description
(IIRC,print
最终会做什么,CBUUID
符合CustomStringConvertible
):
service.uuid.description
或者使用字符串插值:
"\(service.uuid)"
debugDescription
也会产生相同的结果,但除非这实际上是为了调试,否则我不会使用它。
推荐阅读
- postgresql - 错误消息:控制到达程序结束而没有返回
- c++ - 当 pthread 链接到共享库而不是主可执行文件时,C++ 线程报告为禁用
- firebase - Firestore:如何使用 Dart 将一个集合的多个子集合写入 Firestore
- c++ - 是否可以从多个函数中收集参数?
- google-sheets - 谷歌表格:零不是零
- arduino - 附加中断频率arduino
- buddypress - Buddypress - 限制活动流上的博客摘录
- android - Android获取包含至少一个媒体文件(如照片/视频)的所有文件夹
- flutter - 我在 flutter 中遇到 findAncestorStateOfType 错误。我该怎么办?我被困在这里
- kotlin - 在 kotlin react 中编写自定义钩子