Swift、CoreBluetooth:服务和特性

Swift, CoreBluetooth: services and characteristics

peripheral didDiscoverServices()peripheral didDiscoverCharacteristicsForService() 中,我正在尝试以下操作:

for service in peripheral.servicesfor characteristic in service.characteristics,仅收到(对于两个调用)此错误:

'[CBService]?' does not have a member named 'Generator'

'[CBCharacteristic]?' does not have a member named 'Generator'

如何解决这两个错误?

这是一个问题,因为在 Swift 中,peripheral.servicesservice.characteristics 是可选属性。编译器告诉你它们可以是 Nil,所以你不能这样做。不幸的是,您看到的错误消息非常含糊,因此您必须进行 Google 搜索才能弄清楚它的含义。

试试这个:for service in peripheral.services ?? []