Dauris Little
  • About
  • Blogging Lyf
  • Contact
  • Portfolio
“Never be limited by other people’s limited imaginations.” – Dr. Mae Jamison

Working w/Environments in Swift

by Dauris / Wednesday, 15 August 2018 / Published in blog, iOS, Programming Languages, Swift

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.

  • Tweet
Tagged under: environments, iOS, programming w/ swift, swift, swift 4
Avatar photo

About Dauris

What you can read next

javascriptL module pattern morphology
UMD & ESM… What the heck is this?
Using Enum w/Swift
Android Rating: In-App Review API

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All rights reserved. 

TOP
This site uses tracking cookies to personalize content and ads. AcceptLearn More