According to Wikipedia, an Environment Variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. Basically, many developers create 3 environments
- Dev
- Preprod
- Prod
Sometimes we need to separate the base URL and do the initial work which should be done for the following state or condition only.
Create enum for environments
if you need a refresher on enum check out the following article
enum Environment { case dev case preprod case prod }
Environment Variable
let currentEnvironment = Environment.dev
Execute using a switch statement
switch currentEnvironment { case .dev: print("execute only if the environment is development") case .preprod: print("execute only if the environment is preprod") case .prod: print("execute only if the environment is prod") default: break }
Execute code now
First, we initialize the currentEnvironment
variable and using conditions use it. Now we can change the baseUrl
in AppDelegate
along with other interesting code.