-1

Although this question has been asked many times in older environments, when I try to run @mbelsky's answer on a device running iOS 11.2, the simulator block of the code below runs, rather than the device block. When I run the code on the simulator, the simulator runs as expected.

@IBOutlet weak var testSimulatorFlag: UILabel! {
    didSet {
        #if IOS_SIMULATOR
            testSimulatorFlag.text = "Compiler thinks this is a simulator"
        #else
            testSimulatorFlag.text = "Compiler thinks this is a device"
        #endif
    }
}

This is a screenshot of how the project's Swift-Compiler, Custom Flags is set:

Screenshot

Here is a sample project that demonstrates the issue. Thanks in advance for any suggestions.

Blake
  • 122
  • 1
  • 7
  • @AshvinGudaliya, I am aware of that question, but I am asking a new question because the suggested answer to the old question isn't working for me. – Blake Dec 18 '17 at 19:07
  • 1
    Are you sure that you followed the instructions in https://stackoverflow.com/a/37124071/1187415 correctly? Unless I am mistaken, you defined the custom flag for the Debug configuration, and not just for the iOS Simulator SDK. – Martin R Dec 18 '17 at 19:09
  • @MartinR, I added a screenshot to make sure I'm implementing the custom flag correctly – Blake Dec 18 '17 at 19:16
  • 1
    @Blake: Unless I a mistaken, the flag must only be defined in the "Any iOS Simulator SDK" row, *not* in the "Debug" row. – Martin R Dec 18 '17 at 19:23
  • @MartinR, yes, you are correct. Removing that solved my issue. Thanks very much! – Blake Dec 18 '17 at 19:29
  • So it *is* a duplicate ... – Martin R Dec 18 '17 at 19:29

1 Answers1

10

I have found the most reliable way is:

#if targetEnvironment(simulator)
    testSimulatorFlag.text = "Compiler thinks this is a simulator"
#else
    testSimulatorFlag.text = "Compiler thinks this is a device"
#endif
Community
  • 1
  • 1
davidethell
  • 11,708
  • 6
  • 43
  • 63
  • Thanks! I'm trying to avoid using that approach since a compiler flag seems more robust, but, if I can't get to the bottom of why the compiler flag isn't producing expected results, then I will use this solution. – Blake Dec 18 '17 at 19:19