Swift, CoreBluetooth: services and characteristics

458 views Asked by At

In peripheral didDiscoverServices() and peripheral didDiscoverCharacteristicsForService() I am trying the following:

for service in peripheral.services and for characteristic in service.characteristics, only to receive (for both the calls) this error:

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

and

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

How do I solve these two errors?

1

There are 1 answers

0
davidgyoung On BEST ANSWER

This is a problem because in Swift, peripheral.services and service.characteristics are optional properties. The compiler is telling you they can be Nil, so you are not allowed to do this. Unfortunately, the error message you see is very very cryptic so you have to do a Google search to figure out what it means.

Try this instead: for service in peripheral.services ?? []