gpt4 book ai didi

ios - CBCentralManager 可以扩展自定义协议(protocol)吗?我们可以覆盖 CBCentralManagerDelegate 吗?

转载 作者:行者123 更新时间:2023-11-29 11:41:23 28 4
gpt4 key购买 nike

//自定义类

public protocol BluetoothManagerProtocol {
var delegate: CBCentralManagerDelegate? {get set}
//var state: CBCentralManagerState { get }

func scanForPeripheralsWithServices(serviceUUIDs: [CBUUID]?, options: [String : AnyObject]?)
func stopScan()
func connectPeripheral(peripheral: CBPeripheral, options: [String : AnyObject]?)
func connectPeripheral(peripheral: BluetoothPeripheral, options: [String : AnyObject]?)
}

extension CBCentralManager : BluetoothManagerProtocol {
public func connectPeripheral(peripheral: CBPeripheral, options: [String : AnyObject]?) {
//
}

public func scanForPeripheralsWithServices(serviceUUIDs: [CBUUID]?, options: [String : AnyObject]?) {
//
}

public func connectPeripheral(peripheral: BluetoothPeripheral, options: [String : AnyObject]?) {
guard let peripheral = peripheral as? CBPeripheral else {
return
}
connectPeripheral(peripheral, options: options)
}
}

extension CBCentralManagerDelegate{
func centralManager(central: BluetoothManagerProtocol, didDiscoverPeripheral peripheral: BluetoothPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {}

func centralManager(central: BluetoothManagerProtocol,didConnectPeripheral peripheral:BluetoothPeripheral) {}

func centralManagerDidUpdateState(central: BluetoothManagerProtocol) {}

func centralManager(central: BluetoothManagerProtocol, didDisconnectPeripheral peripheral: BluetoothPeripheral, error: NSError?) {}
}

我刚刚进入了 Swift 中协议(protocol)和委托(delegate)的高级主题。我很惊讶,CBCentralManager 可以扩展自定义协议(protocol)吗?如何使用自定义协议(protocol)对 CBCentralManagerDelegate 方法进行参数化?这背后的概念是什么?究竟需要什么?

这是用 swift 2.3 编写的。这个策略在 Swift 4.0 中有效吗?

最佳答案

是的,CBCentralManager 来自 Core Bluetooth 框架,可以包含自定义协议(protocol)定义。遵循此方法以利用 TDD - 测试驱动开发。

由于单元测试蓝牙功能使得同步设备变得困难,开发人员通过创建自己的自定义方法而不是使用 Apple 为 iOS 框架提供的方法来利用依赖注入(inject)来模拟方法。

您可以为 UIView、UIColor 等包含您自己的自定义方法。

例如

class MockCBCentralManager : BluetoothManagerProtocol {
var delegate: CBCentralManagerDelegate?
var scanCalled: Bool = false
var connectPeripheralCalled = false
fileprivate var internalState: CBCentralManagerState = .unknown
var state: CBCentralManagerState {
get {
return internalState
}
}
}
func scanForPeripheralsWithServices(_ serviceUUIDs: [CBUUID]?, options[String : AnyObject]?)
{
scanCalled = true
let advertisementData =
[CBAdvertisementDataServiceUUIDsKey :
[STUDENT_APP_UUID],CBAdvertisementDataLocalNameKey:"MockPeripheral"]

let mock = MockPeripheral()
(delegate as? CentralManager)?.centralManager(self,
didDiscoverPeripheral: mock, advertisementData:
advertisementData,RSSI: 90)

}

更多信息可以在 https://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future

干杯!

关于ios - CBCentralManager 可以扩展自定义协议(protocol)吗?我们可以覆盖 CBCentralManagerDelegate 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369445/

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