我正试图从找到的CBService中读取CBCharacteristic值,并不断得到错误

fatal error: unexpectedly found nil while unwrapping an Optional value

我的代码如下
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!)
{
    for characteristic in service.characteristics as! [CBCharacteristic]
    {
        peripheral.readValueForCharacteristic(characteristic)
    }
}

检查以确保CBService不是nil,并且看起来是健全的。有没有人知道用什么正确的快捷方式来打开这个清单?

最佳答案

多谢狗粮!

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!)
{
    if let characteristics = service.characteristics as? [CBCharacteristic]
    {
        for characteristic in characteristics
        {
            peripheral.readValueForCharacteristic(characteristic)
        }
    }
}

关于xcode - 在For In循环中展开 optional 值时意外发现nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30370085/

10-10 20:53