0

Here's some simple code to show the problem I am having:

import UIKit

let TEST = true
print(TEST)
#if TEST
    print("1. TEST should be true. Value is : ", TEST)
#endif

#if !TEST
    print("2. TEST should be false.  Value is : ", TEST)
#endif

Regardless of whether TEST is 'true' or 'false', the '#if !TEST' is always executed. The '#if TEST' branch is never executed.

This happens in a Playground and in compiled code. Xcode 9 Beta 5

Nishant Bhindi
  • 2,242
  • 8
  • 21
Simon Youens
  • 177
  • 1
  • 13

1 Answers1

1

You are confusing between TEST the local variable and TEST the compiler flag.

  • What you have is a local variable named TEST
  • What you are testing for is the existence of a compiler flag named TEST

To set a compiler flag, go to the target's Build Settings and search for Other Swift Flags, then add -DTEST to your Debug build.

Also, Swift compiler flags are much simpler compared to C compiler flags: a flag either exists or not. You can't associate an int value or anything like that. Consequently, there's no point in getting its value. It's a true/false situation: you know that it exists or not; there's no other value associated with it.

Code Different
  • 90,614
  • 16
  • 144
  • 163
  • CD, thanks for your response I had suspected something like that, but i couldn't find any good description. If I want more than one flag, do they go on the same line? – Simon Youens Aug 26 '17 at 15:39