import Foundation enum TemperatureValue { case notFound case invalidMeasurement case value(Double) init(byte: UInt8) { switch byte { case 0: self = .notFound case 1: self = .invalidMeasurement default: self = .value(Double(byte) * 0.5 - 40) } } var optionalValue: Double? { if case .value(let val) = self { return val } return nil } var isValid: Bool { if case .value = self { return true } return false } var text: String { switch self { case .notFound: return "No sensor" case .invalidMeasurement: return "Invalid" case .value(let value): return String(format:" %.1f°C", value) } } }