How to convert type Data to string or uint8

Hello,
I am reading the value of a characteristic sent from an Arduino (peripheral) via Bluetooth. However, when attempting to print the value, it only prints an empty string. I cannot even compare the value to another variable (integer or string) because it cannot be converted from type Data. I have tried turning it into a UTF8 (string), ascii string, etc. but nothing seems to be working.
Here is my code and some things I've tried:

Code Block
import UIKit
import CoreBluetooth
//local central device object
class BluetoothViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {
//properties
private var centralManager: CBCentralManager!
private var peripheral: CBPeripheral!
//label to display stage of gait cycle
@IBOutlet weak var StateLabel: UILabel!
//characteristics
private var StateVar: CBCharacteristic?
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
//a bunch of core bluetooth functions connecting to peripheral and finding the Arduino's serivce...
//handle discovery of characteristics... subscribe to notifications...
//notification update
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
// Handle error
print(error)
return
}
// Successfully subscribed to or unsubscribed from notifications/indications on a characteristic
}
//read value
func readValue(characteristic: CBCharacteristic) {
self.peripheral.readValue(for: characteristic)
}
//value returned from readValue
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
// Handle error
print(error)
return
}
//var charASCIIval = NSString()
//guard let StateVar = characteristic.value else { return }
guard let StateVar = characteristic.value,
let stringFromData = String(data: StateVar, encoding: .utf8) else { return }
//let stringVal = String(decoding: StateVar, as: UTF8.self)
//let stringVal = "\u{StateVar}"
//let stringVal = String(UnicodeScalar(StateVar))
//let StateVar = string.StateVar(using: .utf8)
print(stringFromData)
StateLabel.text = stringFromData
//if([readCBUUIDs containsObject: [ArduinoPeripheral.ArduinoCharacteristicUUID]])
//let ASCIIstring = NSString(data: StateVar, encoding: String.Encoding.utf8.rawValue) //else { return }
//charASCIIval = ASCIIstring
//guard let StateVarInt = StateVar as? UInt8 else { return }
//StateLabel.text = String(data: StateVar, encoding: String.Encoding.ascii)!
//StateLabel.text = "Value updated"
//let StateVar = StateVar as! UInt8
//State machine
if StateVar == 0 {
StateLabel.text = "Leg Hold"
}
else if StateVar == 1 {
StateLabel.text = "Swing"
}
else if StateVar == 2 {
StateLabel.text = "Heel Step"
}
else if StateVar == 3 {
StateLabel.text = "Drag Heel"
}
else if StateVar == 4 {
StateLabel.text = "Toe Off"
}
else if StateVar == 5 {
StateLabel.text = "Drag Toe"
}
else if StateVar == 6 {
StateLabel.text = "Stance"
}
else if StateVar == 7 {
StateLabel.text = "Drag All"
}
else if StateVar == 8 {
StateLabel.text = "Leg Moving Backwards"
}
}
// init<T> (_ source T) where T: BinaryInteger
// init init(clamping:)
}


I would like to compare the value of the characteristic to a number (0-8) so I can output the correct stage of the gait cycle but I get the error:
Referencing operator function '==' on 'BinaryInteger' requires that 'Data' conform to 'BinaryInteger'

Please help!! Thank you!

How to convert type Data to string or uint8
 
 
Q