Reputation: 1209
I'm working on a project including CoreBluetooth. I researched about that topic and found some tutorials on the web.
Now I recreated some of these tutorials and in almost every method I'm getting the following error:
Initializer for conditional binding must have Optional type, not 'CBCentralManager'
func centralManagerDidUpdateState(central: CBCentralManager) {
if let central = central{ //Here is the error line
if central.state == CBCentralManagerState.PoweredOn {
print("Bluetooth ON")
}
else {
// Can have different conditions for all states if needed - print generic message for now
print("Bluetooth switched off or not initialized")
}
}
}
Upvotes: 0
Views: 202
Reputation: 114846
The CBCentralManager
that is passed to the delegate method is not an optional - it has no ?
suffix, so you don't need to unwrap it. This is what the error is telling you - you are trying to unwrap a variable that is not an optional.
You can just say
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == CBCentralManagerState.PoweredOn {
print("Bluetooth ON")
}
else {
// Can have different conditions for all states if needed - print generic message for now
print("Bluetooth switched off or not initialized")
}
}
Upvotes: 2