我的应用程序在iOS12.4中工作正常,但在iOS 11中崩溃

问题描述 投票:-2回答:1

我的应用程序在iOS 12.4中可以正常运行,但在iOS 11中崩溃。

以下代码在iOS 11中返回nil。

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
{
    if characteristic == rxCharacteristic
    {
        if let ASCIIstring = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue)
        {
            characteristicASCIIValue = ASCIIstring
            print("Value Recieved: \((characteristicASCIIValue as String))")
            NotificationCenter.default.post(name:NSNotification.Name(rawValue: "Notify"), object: nil)

        }
    }
}
ios swift core-bluetooth
1个回答
0
投票

每次使用强制展开时,当值不是您期望的值时,您都在寻找崩溃,因此请避免:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
{
    if characteristic == rxCharacteristic
    {
        if let value = characteristic.value, let ASCIIstring = NSString(data: value, encoding: String.Encoding.utf8.rawValue)
        {
            characteristicASCIIValue = ASCIIstring
            print("Value Recieved: \((characteristicASCIIValue as String))")
            NotificationCenter.default.post(name:NSNotification.Name(rawValue: "Notify"), object: nil)

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.