gpt4 book ai didi

ios - CBCentralManager iOS10 和 iOS9

转载 作者:IT王子 更新时间:2023-10-29 05:39:25 26 4
gpt4 key购买 nike

所以我要迁移到 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
}
}

关于ios - CBCentralManager iOS10 和 iOS9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39450534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com