【发布时间】:2020-11-02 16:16:33
【问题描述】:
我有 2 个 ViewController(ViewController 和 SecondViewController)。
SecondViewController 是一个 tableView,呈现为 popover。 tableView 将列出所有发现的 BLE 设备,当用户点击单元格时,它将连接到选定的设备。
在用户关闭弹出框之前一切正常,当弹出框关闭时将立即打印以下错误。 [CoreBluetooth] XPC 连接无效
我正在使用协议委托来传递外围设备和特征
这就是我在 SecondViewController 中的内容:
protocol PassDataDelegate {
func passPeripheral(_ deviceToConnect: CBPeripheral!)
func passCharacteristic(_ char: CBCharacteristic!)
}
class SecondViewController: UIViewController, CBPeripheralDelegate {
...
var peripherals = Array<CBPeripheral>()
var deviceToConnect: CBPeripheral?
var char: CBCharacteristic?
var deviceReady: Bool?
var delegate: PassDataDelegate?
...
}
//passing variables
delegate?.passPeripheral(deviceToConnect!)
delegate?.passCharacteristic(char!)
这就是我的 ViewController 中的内容:
extension ViewController: PassDataDelegate {
func passPeripheral(_ device: CBPeripheral!) {
self.device = device
}
func passCharacteristic(_ characteristic: CBCharacteristic!) {
self.characteristic = characteristic
self.deviceReady = true
}
}
在我的 ViewController 的某个地方,我在 If 语句中使用了 deviceReady。但它表明 Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value。
好像没有调用协议函数?
请问我可以帮忙吗?
【问题讨论】:
-
SecondViewController解散后是否取消初始化?我认为delegate应该是weak属性。 -
@vpoltave 不,它没有取消初始化。感谢您的评论,您对如何从 popover 传递外设和特性有什么想法吗?
-
您能说明您分配此
var delegate: PassDataDelegate?属性的位置吗?
标签: ios swift xcode core-bluetooth