的值转换为预期类型

的值转换为预期类型

本文介绍了转换为Swift 3.0语法后,如何将类型“ CBManagerState”的值转换为预期类型“ CBCentralManagerState”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an iOS App which uses the CoreBluetooth Central Manager. The app was working as expected, until I updated to xCode 8. This update somehow forced me to convert the code from Swift 2.3 to Swift 3.0 with the conversion manager.After this, I got the error message 'cannot convert value of Type 'CBManagerState' to expected argument type 'CBCentralManagerState' and I was searching for an answer, but due to the reason the update is new, there aren't any helpful issues or documentation regarding the CB Bluetooth used with Swift 3.0 or iOS 10.0.

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

The lines marked with a star are the lines which produced the error.

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) *
   }
 }

我们将不胜感激。
预先感谢。

Any help is appreciated.Thanks in advance.

推荐答案

这对我来说是编译的:

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

根据:

我只使用
centralManagerDidUpdateState func-但操作如下:

I'm only using the state in thecentralManagerDidUpdateState func - but doing so as follows:

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.poweredOn

Which the compiler seems to be happy with (ie - removing the preceding CBCentralManager from CBCentralManager.poweredOn

这篇关于转换为Swift 3.0语法后,如何将类型“ CBManagerState”的值转换为预期类型“ CBCentralManagerState”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:07