如何在方法之间传递潜在错误

时间:2019-01-03 00:28:39

标签: swift core-bluetooth

我正在尝试包装Core Bluetooth Peripheral方法以在React Native中使用。与已经完成的android代码相对应,因此设置了API。

当我呼叫 CBPeripheralManager.addService 时,我需要履行或拒绝从javascript端发出的承诺。

问题是,Core Bluetooth没有提供该方法的回调,似乎期望private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?)

我是iOS和Swift的新手,所以这种行为对我来说很奇怪。有什么想法可以包装该函数,以便我可以正确处理错误报告吗?

谢谢

class BLE: NSObject, CBPeripheralManagerDelegate {
    var advertising: Bool = false
    var servicesMap = Dictionary<String, CBMutableService>()
    var manager: CBPeripheralManager!

    override init() {
        super.init()
        manager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    }

    func addService(promise, serviceUUID) {
        let serviceUUID = CBUUID(string: uuid)
        let service = CBMutableService(type: serviceUUID, primary: true)
        manager.add(service)
    }

    private func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: Error?) {
        if let error = error {
            // this should reject the addService promise
            return
        }
        // this should fulfill the promise
    }    
}

1 个答案:

答案 0 :(得分:0)

尚不清楚error[E0506]: cannot assign to `tmp` because it is borrowed --> empty.rs:109:13 | 109 | tmp = match tmp.as_mut(){ | ^ --- borrow of `tmp` occurs here | _____________| | | 110 | | Some(_n) => { 111 | | let sum = node1.val + node2.val + _n.val; 112 | | let carry = sum /10; ... | 121 | | _ => unreachable!(), 122 | | }; | |_____________^ assignment to borrowed `tmp` occurs here error[E0499]: cannot borrow `*tmp` as mutable more than once at a time --> empty.rs:109:25 | 109 | tmp = match tmp.as_mut(){ | ^^^ mutable borrow starts here in previous iteration of loop ... 126 | } | - mutable borrow ends here error[E0505]: cannot move out of `res` because it is borrowed --> empty.rs:115:28 | 105 | let mut tmp = &mut res; | --- borrow of `res` occurs here ... 115 | return res; | ^^^ move out of `res` occurs here 的类型是什么,但是您需要将其存储在某个地方,然后再实现。例如,您可以添加一个属性:

promise

(我不知道您所承诺的类型确实在这里)

然后将其存储在var pendingServices: [CBUUID: Promise] = [:] 中:

addService

然后在(正确的;请参见我的评论)委托方法中,您将对其进行处理:

assert(pendingServices[serviceUUID] == nil)
pendingServices[serviceUUID] = promise