Skip to content

Instantly share code, notes, and snippets.

@hamsternik
Created March 21, 2018 18:27
Show Gist options
  • Select an option

  • Save hamsternik/e5e641f5f1e5b2027fd89e0c2d647a18 to your computer and use it in GitHub Desktop.

Select an option

Save hamsternik/e5e641f5f1e5b2027fd89e0c2d647a18 to your computer and use it in GitHub Desktop.
import UIKit
import CoreBluetooth
enum BluetoothState: CustomStringConvertible {
case off, on, unknown
init(bluetoothState: CBManagerState) {
switch bluetoothState {
case .poweredOff: self = .off
case .poweredOn: self = .on
default: self = .unknown
}
}
var description: String {
switch self {
case .off: return "Turned off"
case .on: return "Turned on"
case .unknown: return "Unknown"
}
}
var color: UIColor {
switch self {
case .off: return .red
case .on: return .green
case .unknown: return .yellow
}
}
}
// MARK: - Case properties
extension BluetoothState {
var off: BluetoothState? {
guard case .off = self else {
return nil
}
return self
}
var on: BluetoothState? {
guard case .on = self else {
return nil
}
return self
}
var unknown: BluetoothState? {
guard case .unknown = self else {
return nil
}
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment