Skip to content

Instantly share code, notes, and snippets.

@getaaron
Created December 15, 2017 20:19
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 getaaron/8babdd451856ed8ebc341cf8313e8b97 to your computer and use it in GitHub Desktop.
Save getaaron/8babdd451856ed8ebc341cf8313e8b97 to your computer and use it in GitHub Desktop.
Basic feature flags in Swift
//: # Feature Flag example
//: ## Implementation
import Foundation
enum Feature: String {
case Login, AdminTool
var key: String {
return "FEATURE_FLAG_\(rawValue.uppercased())"
}
var isEnabled: Bool {
guard let flag = ProcessInfo().environment[key] else { return false }
return flag == "on"
}
func toggledOn(block: ()->()) {
if isEnabled {
block()
}
}
func toggledOff(block: ()->()) {
if !isEnabled {
block()
}
}
}
//: ## Usage Example
Feature.Login.toggledOn {
print("do this if feature is on")
}
Feature.Login.toggledOff {
print("do this if feature is off")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment