【问题标题】:Swift - BTLE - How to Add CBCentralManagerDelegate ProtocalSwift - BEETLE - 如何添加 CBCentralManagerDelegate 协议
【发布时间】:2014-08-06 10:04:17
【问题描述】:

我想制作一个使用蓝牙进行通信的 ios 应用程序。我正在使用 swift。

首先我添加CoreBluetooth.framework,然后我添加一个bridge.h并将文件添加到系统桥,导入“CoreBluetooth/CoreBluetooth.h”。

然后我新建一个类,

import UIKit

class BTCentral: NSObject, CBCentralManagerDelegate   {

}

我计划在该类中创建一个蓝牙 CBCentralManager。但是,上面的代码给了我一个错误。

Type 'BTCentral' does not conform to protocol 'CBCentralManagerDelegate' 

这是一个特定的屏幕截图:click here

任何帮助将不胜感激!非常感谢你们的时间。

【问题讨论】:

  • 为什么要通过桥接头导入CoreBluetooth?它应该可以作为一个模块使用,所以你应该可以在你的 Swift 文件中使用import CoreBluetooth。 (如果这不起作用,您应该file a bug。)
  • 那行得通。 @rickster 感谢您的提示。我还在学习开发 ios 应用程序。
  • 对不起我的评论。我想知道为什么在这种情况下需要扩展 NSObject 。如果我不扩展 NSObject 这个错误继续。最好的问候
  • @Arubu CBCentralManager 的委托必须是 NSObject。我希望这将在未来的 CoreBluetooth 版本中得到解决。

标签: swift core-bluetooth


【解决方案1】:

您必须实现协议所需的所有方法。在这种情况下,CBCentralManagerDelegate 只需要实现一种方法,centralManagerDidUpdateState()

将此方法添加到您的类中,错误就会消失。

func centralManagerDidUpdateState(central: CBCentralManager!) {

}

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多