【发布时间】:2018-03-04 07:29:37
【问题描述】:
// 自定义类
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 协议和委托的高级主题。我很惊讶,CBCentralManager 可以扩展自定义协议吗? 如何使用自定义协议对 CBCentralManagerDelegate 方法进行参数化? 这背后的概念是什么?究竟是什么需求?
这是用 swift 2.3 编写的。这种策略是否适用于 Swift 4.0?
【问题讨论】:
-
欢迎来到 SO!我们的社区最适合处理写得好的问题。请看一下here 请只问一个具体问题。
标签: ios objective-c xcode swift3 core-bluetooth