gpt4 book ai didi

ios - 转换为Swift 3.0语法后,如何将类型“CBManagerState”的值转换为预期类型“CBCentralManagerState”?

转载 作者:行者123 更新时间:2023-12-01 18:07:18 25 4
gpt4 key购买 nike

我正在使用CoreBluetooth Central Manager的iOS应用程序上工作。该应用程序按预期运行,直到我更新到xCode8。此更新以某种方式迫使我使用转换管理器将代码从Swift 2.3转换为Swift 3.0。
此后,我收到错误消息“无法将类型“CBManagerState”的值转换为预期的参数类型“CBCentralManagerState”,并且我正在寻找答案,但是由于更新是新的原因,因此没有任何有用的问题或与Swift 3.0或iOS 10.0一起使用的CB蓝牙的文档。

标有星号的线是产生错误的线。

final class BluetoothSerial: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
....//some code here from HM10 Bluetooth Serial
var centralManager: CBCentralManager!
var state: CBCentralManagerState { get { return centralManager.state } *

func centralManagerDidUpdateState(_ central: CBCentralManager) {
//note that "didDisconnectPeripheral" won't be called if BLE is turned off while connected

connectedPeripheral = nil
pendingPeripheral = nil

//send it to the delegate
delegate.serialDidChangeState(central.state) *
}
}

任何帮助表示赞赏。
提前致谢。

最佳答案

这为我编译:

var state: CBCentralManagerState { get { return CBCentralManagerState(rawValue: centralManager.state.rawValue)! }

根据 dev forums:

枚举是二进制兼容的,因此您的代码可以在任何iOS版本上正常运行

我只是在使用状态 centralManagerDidUpdateState函数-但是这样做如下:
switch central.state{
case .poweredOn:
NSLog("CoreBluetooth BLE hardware is powered on");
break
case .poweredOff:
NSLog("CoreBluetooth BLE hardware is powered off");
break;
case .unauthorized:
NSLog("CoreBluetooth BLE state is unauthorized");
break
case .unknown:
NSLog("CoreBluetooth BLE state is unknown");
break;
case .unsupported:
NSLog("CoreBluetooth BLE hardware is unsupported on this platform");
break;
default:
break
}

编译器似乎对此感到满意的(即,从 CBCentralManager中删除前面的 CBCentralManager.poweredOn

关于ios - 转换为Swift 3.0语法后,如何将类型“CBManagerState”的值转换为预期类型“CBCentralManagerState”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577272/

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