从委托回调中创建异步函数

时间:2021-06-22 18:09:21

标签: swift asynchronous

我想在 Core Bluetooth 之上实现一个 async/await 接口。我想说

let response = await sendBluetoothCommand(.myCommand)

现在,要通过 BLE 发送,我写

peripheral.writeValue(bleCommand.payload, for: writeCharacteristic, type: .withResponse)

然后我在委托函数中得到响应

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { ... }

让我们假设我想要的答案是来自特性的 NSData,所以我在这里可能想要的是 Task.Handle

如何在 one 函数中完成此任务并在委托函数中完成它,以便它可以像第一个示例中那样工作?

0 个答案:

没有答案