Skip to content

Instantly share code, notes, and snippets.

@taimila
Last active June 25, 2019 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taimila/733917f0f3e50609c39688fa02c9fb72 to your computer and use it in GitHub Desktop.
Save taimila/733917f0f3e50609c39688fa02c9fb72 to your computer and use it in GitHub Desktop.
Feature Toggle implementation in Swift
import Foundation
enum Feature: String, CaseIterable {
case newThing
case otherThing
var isEnabled: Bool {
return UserDefaults.standard.bool(forKey: "feature-\(self.rawValue)")
}
func enable() {
UserDefaults.standard.set(true, forKey: "feature-\(self.rawValue)")
}
func disable() {
UserDefaults.standard.set(false, forKey: "feature-\(self.rawValue)")
}
func toggle() {
if self.isEnabled {
disable()
} else {
enable()
}
}
static func enableAll() {
for feature in self.allCases {
feature.enable()
}
}
static func disableAll() {
for feature in self.allCases {
feature.disable()
}
}
}
/// Examples how to use Feature toggle implementation above.
func exampleUsage() {
if Feature.newThing.isEnabled {
print("New Thing is enabled")
}
Feature.otherThing.enable()
Feature.enableAll()
Feature.disableAll()
Feature.newThing.toggle()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment