5

I am trying to figure out how to handle variables/constants for different environments, e.g. development(or debug) and release. For instance when executing a unit test the url for a web service should point to the localhost, but in the final product it should point to the public api host.

I have read something about setting the Swift Compiler - Custom Flags Debug settings to -DDEBUG and then in the code declare the variable like so:

#if DEBUG
  let url = "http://localhost"
#else
  let url = "https://api.example.com"
#endif

But that didn't work. When running a unit test the url is never set to http://localhost. Did I miss something here?

Gob
  • 305
  • 2
  • 11

1 Answers1

3

Edit project scheme...

enter image description here

Define your environment variable: enter image description here

And finally check if was defined for the schema that you are dealing with:

var baseURL:String{
    get{
        if let _ = ProcessInfo().environment["LOCAL_MOCK_SERVER"]{
            return "http:/localhost:3000"
        } else{
            return "https://api.fixer.io"
        }

    }
}