My app uses a lib that won't build and/or run on a simulator so I effectively stubbed out the references to that lib by surrounding references with preprocessor directives like so:
#if !(TARGET_IPHONE_SIMULATOR)
//Do the real implementation
#else
//Do a dummy implementation for testing
XCode automatically checks to see what my current target is and evaluates the #if/#else which I guess is kind of nice. The problem is, it turns off syntax highlighting, autocomplete, etc for whichever condition is not going to be compiled. (For example if my current target is the simulator, the code inside the real implementation loses its highlighting)
My bad solution is changing the target so that whichever implementation I want to edit gets 'activated'. Is there a way to keep both of them highlighted at all times so I can seamlessly edit both?
If both implementations will compile for either target, then you could do something like this:
#if !(TARGET_IPHONE_SIMULATOR)
const bool simulator = false;
#else
const bool simulator = true;
#endif
if (simulator)
{
//Do a dummy implementation for testing
}
else
{
//Do the real implementation
}
Both branches will be compiled, although the optimizer should throw away the one which can never be used for a given target.
If code using the library can't even be built when the target is the simulator, you can do something like this:
#if !TARGET_IPHONE_SIMULATOR
if (true)
{
//Do the real implementation
}
else
#endif
{
//Do a dummy implementation for testing
}
In this case, the real implementation still won't be syntax colored or support completion when targeting the simulator, but both branches will have those when targeting the device.
You could also implement a dummy version of the library for the simulator. You would have it define all of the interfaces, but everything would just return failure or even throw exceptions. You would use #if TARGET_IPHONE_SIMULATOR
around all of the code so that the library ends up completely empty for device builds. Then, you would link against that and use the first approach.
来源:https://stackoverflow.com/questions/29497226/xcode-syntax-highlighting-in-both-conditions-of-preprocessor-if-else