Last active
December 27, 2017 06:00
-
-
Save TerryCK/5ee81fbe3bc24b09d19b4bbf9f327340 to your computer and use it in GitHub Desktop.
iOSTaipei
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol BatteryStatusProtocol { | |
static var allStatus: [BatteryStatus] { get } | |
mutating func setStatus<T: BinaryInteger>(input: T) | |
} | |
enum BatteryStatus: String, BatteryStatusProtocol { | |
case full, medium, less, charging, notOffcial | |
} | |
// Implement | |
extension BatteryStatus { | |
static var allStatus: [BatteryStatus] { | |
return [full, medium, less, charging, notOffcial] | |
} | |
static let shared: BatteryStatus = .full | |
mutating func setStatus<T: BinaryInteger>(input: T) { | |
switch input { | |
case 0...30: self = .less | |
case 31...90: self = .medium | |
case 90...100: self = .full | |
default: self = .notOffcial | |
} | |
} | |
} | |
// Usage | |
var batteryStatus = BatteryStatus.shared | |
batteryStatus.setStatus(input: 50) | |
batteryStatus.rawValue | |
batteryStatus.setStatus(input: 10) | |
batteryStatus.setStatus(input: -1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment