21

I added TEST=1 in the Preprocessor Macros section of the project target's Build Settings, and whenever I use it in code, the preprocessor thinks it's not defined.

  1. alt + click on it shows a question mark
  2. The syntax coloring doesn't work well
  3. The syntax autocomplete doesn't work well

For example, having:

#if TEST

// a

#else

// b

#endif

has this behavior:

  1. Syntax coloring only works for // b
  2. Syntax autocomplete only works for // b

This is because the preprocessor can't find TEST (even if it's defined as 1, as I previously stated) so it treats it as being false.

Is there any way to solve these problems?

Edit:

My use case is related to multiple Project Targets. So, say that the initial target is named First, which has the TEST preprocessor macro defined with value 1.

If I create another project target named Second with no preprocessor macros defined and want to add some code for it only if TEST is 0 (false/not defined), I'll put it in the #else block (// b).

Knowing that syntax coloring and autocomplete works for // b, it looks like it works ok, but if I change the current target to the First one (pun intended) and even build it, it still works as Second is selected.

4
  • if you build the project, preprocessor will detect the compile time flags and will show coloring and autocomplete. have you tried that? Commented Mar 9, 2017 at 3:56
  • Of course I built the project. Commented Mar 9, 2017 at 8:55
  • Did you do it for both debug and release? It works at my end. Commented Mar 9, 2017 at 12:18
  • This has nothing to do with Build Configurations. I updated my question. Commented Mar 9, 2017 at 13:04

3 Answers 3

19

If you want to do it like this in Swift, you should add a User-Defined setting named OTHER_SWIFT_FLAGS in your Build Settings like this:

OTHER_SWIFT_FLAGS user-defined setting

And then in your code you type:

#if TEST1

// Debug

#endif

#if TEST2

// Release

#endif

I don't know why you need this, but it may be interesting to look into native Xcode Target, to use specific behavior for multiple apps with same root project. If this is what you want, you should look into an article this: AppCoda using Targets with Xcode.

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

Comments

5

Just go to target->Edit Scheme -> Run -> Info and check the Build Configuration. Now while adding the Preprocessor Macros in build setting just make sure you are adding the macro for correct build configuration and correct target.

≈

I tried by adding for both debug and release build as above for my target App (Test-ObjectiveC). So when i tried below code with this setting, u can see the syntax color accordingly.

enter image description here

3 Comments

And what happens when you cmd + click it?
@lulian not sure what you want to check there with cmd + click.
I was just wondering if it knows to open the Build Settings or if it's showing the question mark as in my example. There might be other settings of my big project that interfere with this. I'll try testing it myself in a new project and try to find if the problem is caused by one of my settings.
4

I was able to make it work by setting "Target->Build Settings->All->Swift Compiler - Custom Flags -> Active Compilation Conditions".

XCode version 12.3

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.