因此,我正在迁移到iOS10,但是我还需要我的代码才能在iOS9上运行。我正在使用CoreBluetooth和CBCentralManagerDelegate。我可以让我的代码在iOS10上运行,但是我也需要后备才能在iOS9上运行。
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if #available(iOS 10.0, *) {
switch central.state{
case CBManagerState.unauthorized:
print("This app is not authorised to use Bluetooth low energy")
case CBManagerState.poweredOff:
print("Bluetooth is currently powered off.")
case CBManagerState.poweredOn:
print("Bluetooth is currently powered on and available to use.")
default:break
}
} else {
// Fallback on earlier versions
switch central.state{
case CBCentralManagerState.unauthorized:
print("This app is not authorised to use Bluetooth low energy")
case CBCentralManagerState.poweredOff:
print("Bluetooth is currently powered off.")
case CBCentralManagerState.poweredOn:
print("Bluetooth is currently powered on and available to use.")
default:break
}
}
}
我得到了错误:
Enum case 'unauthorized' is not a member of type 'CBManagerState'
在线上:
case CBCentralManagerState.unauthorized:
以及.poweredOff和.poweredOn。
有什么想法可以使它在两种情况下都能正常工作吗?
最佳答案
您可以简单地省略枚举类型名称,而只需使用.value。由于基础原始值兼容,因此可以在没有警告的情况下进行编译,并且可以在iOS 10及更早版本上使用。
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state{
case .unauthorized:
print("This app is not authorised to use Bluetooth low energy")
case .poweredOff:
print("Bluetooth is currently powered off.")
case .poweredOn:
print("Bluetooth is currently powered on and available to use.")
default:break
}
}