Created
December 15, 2017 20:19
-
-
Save getaaron/8babdd451856ed8ebc341cf8313e8b97 to your computer and use it in GitHub Desktop.
Basic feature flags in Swift
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
//: # 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