我的应用程序在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)
}
}
}
每次使用强制展开时,当值不是您期望的值时,您都在寻找崩溃,因此请避免:
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)
}
}
}