segue中丢失的数据(xcode8.0)

我是swift的初学者,并尝试开发简单的程序,显示BLE的特征 . 我通过打印确认我捕获了目标特征的UUID

myCharacteristicUUID_1 : (
    "Manufacturer Name String",
    "Model Number String",
    "Serial Number String",
    "Hardware Revision String",
    "Firmware Revision String",
    "Software Revision String"
)

但是,在覆盖功能准备(对于segue:UIStoryboardSegue,发件人:任何?),我再次通过打印检查它,我看到数据已经消失如下;

myCharacteristicUUID_2 : (
)

我可以移交其他类型的值(例如字符串,文本等)但我无法处理从BLE捕获的数据 . 有人可以指出当我在segue中使用BLE数据时我必须做什么吗?以下是我的代码;

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor targetService: CBService, error: Error?) {

    if error != nil {
        print("Characteristic NOT found : \(error)")

    }

    let foundCharacteristics = targetService.characteristics

    for c in foundCharacteristics! as [CBCharacteristic] {

        myCharacteristicUUID.add(c.uuid)

    }

    print("myCharacteristicUUID_1 : \(myCharacteristicUUID)")

}

// prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "mySegue") {

        print("myCharacteristicUUID_2 : \(myCharacteristicUUID)")

        let mySecondViewController : SecondViewController = segue.destination as! SecondViewController

        mySecondViewController.relayedFoundCharacteristicUUID = myCharacteristicUUID

     }

}

回答(1)

3 years ago

这就是为什么没有正确读取 myCharacterisitcUUID 的原因 . 请看 performSegue 的地方 .

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    selectedService = myService[indexPath.row] as! CBService
    print("Selected_service : \(selectedService)")

    self.peripheral.discoverCharacteristics(nil, for:selectedService as CBService!)

    //performSegue(withIdentifier: "mySegue", sender: nil)
    // I used to performed segue here. This was the reason myCharacterisitcUUID was empty.


}

// find the characteristics for the targetService

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor targetService: CBService, error: Error?) {

    if error != nil {
        print("Characteristic NOT found : \(error)")

    }

    foundCharacteristics = targetService.characteristics!

    for c in foundCharacteristics as [CBCharacteristic] {

        self.myCharacteristicUUID.add(c.uuid)

    }
    // segue should be performed here ! This change worked!
    performSegue(withIdentifier: "mySegue", sender: nil)
}