13

I am trying to use different environments for my application but facing issues doing it. It's not the case that debug is not working the case is none of the variables are working.

I am attaching a screenshot which will make it easier to understand what is happening.

I am not able to access any of the environments.

I ran this code in viewDidLoad of initial view controller of my application and last else condition gets executed.

#if DEBUG
    print("Environment is debug")
    #elseif debug
    print("Environment is debug")
    #elseif Debug
    print("Environment is Debug")
    #elseif RELEASE
    print("Release")
    #elseif PRODUCTION
    print("Environment is production")
    #elseif PROD
    print("Environment is PROD")
    #else
    print("Environment is can't find")
#endif

io

Adding few more images for better clarity regarding schemes and Xcode settings.

enter image description here

enter image description here

user3745635
  • 847
  • 1
  • 8
  • 21

4 Answers4

31

In my case the issue was solved by adding DEBUG as the debug Active Compilation Condition. I know it's already specified when you create a new project, but I don't remember if me or another team's member removed it (and why!). So I decided to put it here just in case someone else is facing the same scenario

DEBUG specification in Active Compilation Conditions

mauricioconde
  • 5,032
  • 3
  • 28
  • 24
18

I did research and setting compiler flags solved the problem.

Earlier they were blank and the way Xcode UI is I got confused how to edit them they looked disabled.

So what you have to do is double tap on the side of the flags or press enter and add the following values as I had attached the screenshot below.

enter image description here

user3745635
  • 847
  • 1
  • 8
  • 21
9

DEBUG is the only default swift flag on a new project. You can create your own in your project build settings, Other Swift Flags.

Otherwise:

#if DEBUG
 // This code will be run while installing from Xcode
#else
// This code will be run from AppStore, Adhoc ... 
#endif
CZ54
  • 5,488
  • 1
  • 24
  • 39
  • 3
    My project was written in Obj-C and Swift has been added. In my case, the DEBUG flag was not automatically created and had to be manually added. – Chuck Krutsinger Aug 11 '21 at 23:47
0

Here's how the Build Settings are supposed to look for Swift & Objective-C. These are the settings that Xcode creates automatically, so if you're missing DEBUG you can add them again

Adding DEBUG to build settings in Xcode

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421