I’ve spent 3 weeks avoiding this query on Stackover stream however I am at my wits finish.
I do not know why I can create a string manually and convert it into an INT however cannot do the identical factor from BLE knowledge.
That is my code:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor attribute: CBCharacteristic, error: Error?) {
if error != nil {
print("ERROR ON UPDATING VALUE FOR CHARACTERISTIC: (attribute) - (String(describing: error?.localizedDescription))")
return
}
guard let knowledge = attribute.worth else{
return
}
// let myString = "345345" // I CAN MANUALLY SET A STRING
// let myInt = Int(myString) ?? 2
// print (myInt + 523436) I CAN CONVERT AND USE THIS INT
let weightStringData = swiftDataToString(ESP32Data: knowledge)
func swiftDataToString(ESP32Data:Knowledge) -> String? {
return String(knowledge: knowledge, encoding: .utf8)
}
if let myNewstring = weightStringData{
// print(myNewstring) // THIS PRINTS AS A STRING
let myInt = Int(myNewstring) ?? 2 //THIS DOES NOTHING IT SEEMS OTHER THAN SET THE CONSTANT TO "2"
print (myInt + 9898) //CANT USE THE INT LIKE IN THE ABOVE MANUAL EXAMPLE
}else{
print("Nothing")
}
}
}