2

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?

1 Answer 1

3
+50

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.

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

1 Comment

Thanks for the answer! I edited my post to clarify that the library won't build on the simulator so I can't do the first solution but great answer; helps a lot

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.