I get this data from BLE, but is crashes on certain values (I think when data is float )
function to get data :
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print(characteristic); // prints a value always
if characteristic.UUID == UUID_CHAR {
if ( characteristic.value == nil)
{return; }
//***crash is here :
let str:NSString = NSString(data: characteristic.value!, encoding: NSUTF8StringEncoding)!
print ("got::",str);
This will crash when the characteristic is :
<CBCharacteristic: 0x157685860, UUID = FFE1, properties = 0x16, value = <00ff00>, notifying = YES>
and not when its this : (value field here is 00 )
<CBCharacteristic: 0x157685860, UUID = FFE1, properties = 0x16, value = <00>, notifying = YES>
I have tried wrapping it with if let
, which did not worked.
ERROR: unexpectedly found nil while unwrapping an Optional value
How would you cast the value to string, knowing it could be a float or integer ?