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

3
  • See stackoverflow.com/questions/24325477/… Commented Aug 23, 2017 at 4:12
  • var TEST = true Commented Aug 23, 2017 at 5:31
  • I changed 'let TEST = true' to 'var TEST = true', but I still get the same erroneous behaviour. Commented Aug 23, 2017 at 16:51

1 Answer 1

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.

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

1 Comment

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?

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.