【问题标题】:Objective-C warning: Class 'ViewController' does not conform to protocol 'CBPeripheralManagerDelegate'Objective-C 警告:“ViewController”类不符合协议“CBPeripheralManagerDelegate”
【发布时间】:2021-02-23 15:35:55
【问题描述】:

我正在尝试创建一个 Obj-C、CoreBluetooth 虚拟外围应用程序并收到此警告。

//
//  ViewController.h
//  sim_backend_empty3
//
//

#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController : UIViewController  <CBPeripheralManagerDelegate>

@property (nonatomic, strong) CBPeripheralManager *peripheralManager;

@end


//
//  ViewController.m
//  sim_backend_empty3
//
//

#import "ViewController.h"

@implementation ViewController   >>>>>>>>>> WARNING >>>>>>>>>>   Class 'ViewController' does not conform to protocol 'CBPeripheralManagerDelegate'

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)start_BLE_advertisements
{
    [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
    
}


@end

【问题讨论】:

  • 一旦您在代码中读取delegate 并将其设置为self,您需要在同一个类中实现其方法。 cmd-click CBPeripheralManagerDelegate-> 跳转到定义并阅读。回到你的课堂上。将光标设置在 @impl 和 @end 之间,然后输入 -pe 看看会发生什么。

标签: objective-c xcode bluetooth-lowenergy core-bluetooth ios-bluetooth


【解决方案1】:

需要实现所需的CBPeripheralManagerDelegate协议方法:

peripheralManagerDidUpdateState:

如文档中所述:https://developer.apple.com/documentation/corebluetooth/cbperipheralmanagerdelegate?language=objc

协议需要一个方法,peripheralManagerDidUpdateState:,每当外围管理器的状态更新时,Core Bluetooth 都会调用该方法,以指示外围管理器是否可用。

【讨论】:

    【解决方案2】:

    您可以查看如何使用此 repo 设置 CoreBluetooth 的 Objective-C 示例:

    https://github.com/LGBluetooth/LGBluetooth

    当我还在使用 ObjC 编码时,它是我使用的库 - 它非常棒。

    至少,它会给你一些关于你应该如何实现接口的想法。

    这是您尝试做的事情的 Swift 实现(基于您几天前提出的其他问题)。也许您可以将其反向移植到 ObjC(免责声明:我是作者)。

    https://github.com/RobotPajamas/SwiftyTeeth/blob/master/Sources/SwiftyTooth/SwiftyTooth.swift

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多