Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Other Swift Flags not recognized in code

Tags:

xcode

swift

I have a Swift project, I added a unit test target to it. my config file Constants.swift contains a struct:

struct Constants {
  struct Network {
    #if APITESTS
    static let serverURL = "https://www.servertest.com"
    #else
    static let serverURL = "https://www.server.com"
    #endif
  }
}

In my test target Other Swift Flags I added -DAPITESTS Then run the unit test with CMD+U In a log present in my test, I can see that it's "https://www.server.com" that is selected here when it should be servertest.

If I use #if DEBUG instead of APITESTS, it works. If I try to add APITESTS within Active Compilation Conditions (just like DEBUG) it is still not recognized.

Any clear explaination?

like image 955
Mikael Avatar asked Aug 31 '17 03:08

Mikael


People also ask

How do I use another swift flag?

To do that, goto Project (not target)→Build settings → Swift Compiler → Custom flags → Other swift flags section and edit the different configuration flags. Add the flags using the syntax “ -D <flag_name>”. NOTE: if you are not seeing Other swift flags section, you will be in the basic build settings mode.

What is Flag in Swift?

Feature flags allow you to slowly rollout a feature, rather than doing a risky big bang launch, and are extremely helpful when used in a continuous integration and continuous delivery environment. At Optimizely, we commonly use feature flags to reduce the risk of complicated deploys like rolling out new APIs.


1 Answers

When you define a condition in the test target, it only affects the test; the main app is built based on its usual configuration. The two easy solutions I see here are to set an environment variable or define a special test configuration in the main app and set the scheme to use that configuration for tests.

enter image description here

enter image description here

enter image description here

enter image description here

like image 64
Kevin Avatar answered Oct 20 '22 18:10

Kevin