0

I want to use global variables for defining the API base path of my app based on the current building configurations (dev, production). These are the current approaches:

1)

     struct ApiSettings {

     static  let API_DEV_BASEPATH = "http://dev.myapp.com"
     static  let API_PRODUCTION_BASEPATH = "http://prod.myapp.com"

    // Return the basepath for the current app runtime mode
    static var API_BASEPATH:String {

        var bPath: String!
        #if DEBUG
            bPath = API_DEV_BASEPATH
        #else
            bPath = API_PRODUCTION_BASEPATH
        #endif
        return bPath
    }
}

2) (Here I define a staging environment too)

#if DEBUG
let API_BASEPATH = "http://dev.app.com"
#elseif STAGING
let API_BASEPATH = "http://staging.app.com"
#elseif RELEASE
let API_BASEPATH = "http://production.app.com"
#endif

but when I try to use API_BASEPATH in my app the compiler complains about an "Unresolved Identifier"

I'm not sure which the most elegant solutions to be adopted. PS. I'm using Swift3

4
  • It would be a much better design to extract all environment-dependant variables into plist files. Commented Sep 16, 2016 at 18:18
  • is there any way of defining the same variable in a .plist file and extracting it based on the build configuration (debug, staging,release) without using code? I'm thinking of extending the code provided in the answer with constant extracted by the .plist Commented Sep 19, 2016 at 10:49
  • Yeah, you make one plist per build config, and change which one gets included with each package Commented Sep 20, 2016 at 14:43
  • ok thanks a lot. What do you think about the suggested answer? Commented Sep 21, 2016 at 10:27

1 Answer 1

2

please try this.

static var API_BASEPATH:String {
        get {
            var bPath: String!
            #if DEBUG
                bPath = API_DEV_BASEPATH
            #else
                bPath = API_PRODUCTION_BASEPATH
            #endif
            return bPath
        }

    }

hope this will fix your confusion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.