Has Swift compilation condition for SwiftUI preview environment?

Viewed 277

Objective C has preprocessor, Swift has compilation conditions that allows to use different code for different environments, for example for debug or release build:

#if DEBUG
    print("debug message")
    doDebugAction()
#else
    doReleaseAction()
#endif

Can I add code that compiles only for SwiftUI preview? Something like:

#if targetEnvironment(swiftUIPreview) 
    static func mock() -> SomeStruct {
        // fill random data
    } 
#endif
1 Answers

Unfortunately it doesn't exist any compilation condition afaik, but you can use ProcessInfo:

ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
Related