简体   繁体   English

Swift 3,展开外设名称(蓝牙BLE)

[英]Swift 3, unwrapping peripheral name (Bluetooth BLE)

I'm developing an iOS app using Swift 3 / X-Code. 我正在使用Swift 3 / X-Code开发iOS应用。 This app requires bluetooth communication and I've read a few tutorials and stuff on getting it to work. 此应用程序需要蓝牙通信,我已经阅读了一些教程和有关使其工作的内容。 So far (UI-wise) everything is working. 到目前为止(UI方面)一切正常。 However I'm having issues with the following, since most of the tutorials I've read are not updated to Swift 3 I believe that might be the issue here: 但是我遇到以下问题,因为我阅读的大多数教程都没有更新到Swift 3,所以我相信这可能是这里的问题:

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    //What to do when it discovers a peripheral, add it to the array list
    //print("Adding peripherals found to array: " + peripheral.name!)
    //peripheralsFound.append(peripheral.name!)
    print("Peripheral found!")

}

The above code has a few lines commented out, I want the peripherals found to be added to an array, however if I un-comment any line with peripheral.name! 上面的代码注释掉了几行,我希望将外围设备添加到数组中,但是如果我取消注释与peripheral.name相关的任何行! on it the app crashes with the error "unexpectedly found nil while unwrapping optional value" 在此应用程序崩溃,并显示错误“在展开可选值时意外找到nil”

I believe it has to do with the peripheral.name! 我相信这与peripheral.name有关! Which was fixed by X-code itself (it added the ! or else it won't compile) I can't make peripheral.name an optional apparently or the app won't compile and I don't know if there's any other way to get the name of the peripherals found, I'd appreciate any guidance. 这是由X代码本身修复的(它添加了!或否则将无法编译),我显然无法将外围设备.name设为可选,否则应用程序将无法编译,并且我不知道是否还有其他方法为了找到外设的名称,我将不胜感激。 Thank you! 谢谢!

You can add a default value to the print statement using ?? 您可以使用??向打印语句添加默认值。 "..." “......”

    print("Adding peripherals found to array: " + (peripheral.name ?? "name was nil"))

This will keep you from having to force it or you can just do a simple check with an if let 这将使您不必强制执行此操作,也可以通过if进行简单检查

  if let unwrappedName = peripheral.name {
    print("Adding peripherals found to array: " + unwrappedName)
  }

I suggest you read about optionals and how they work so you don't have to rely on the IDE to do all the corrections for you. 我建议您阅读有关可选的内容以及它们如何工作的信息,这样您就不必依靠IDE来为您做所有更正。

Use optional binding to safely unwrap the optional value. 使用可选绑定安全地展开可选值。

var peripheralNames = [String]()

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    if let name = peripheral.name {
        peripheralNames.append(name)
        print("Adding peripheral found to array: " + name)
    }
    print("Peripheral found!")
}

Swift 3: Try This 斯威夫特3:尝试一下

var arrayPeripheral: [CBPeripheral] = []
var arrayPeripheralStringName:[String] = []

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){

    let peripheralLocalName_advertisement = ((advertisementData as NSDictionary).value(forKey: "kCBAdvDataLocalName")) as? String

    if (((advertisementData as NSDictionary).value(forKey: "kCBAdvDataLocalName")) != nil){

        if !(self.arrayPeripheral.contains(peripheral)){

            self.arrayPeripheral.append(peripheral)

            self.arrayPeripheralStringName.append(peripehral.name)               

            peripheral.delegate = self
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM