Share data with multiple SwiftUI Preview macros without data remaining in Release build config

76 views Asked by At

I am looking for how to do the equivalent of this:

#Preview("light") {
    BillView(bill: prevBill)
}

#Preview("dark") {
    BillView(bill: prevBill)
        .preferredColorScheme(.dark)
}

// I made this macro name up.
// My question is asking for the equivalent functionality.
#SharedPreviewStuff {
    // prevBill constant should *not* appear in Release build.
    let prevBill = Backend.Bill(initString: "Blah")
}

In other words, is there some way to tag code or wrap it in a macro such that it gets removed from the app at the same time as the previews do?

I cannot put #if DEBUG around prevBill definition because build will fail during Archive operation (Release build configuration). This is because, apparently, #Preview macros cause the preview itself to be removed at a later phase or perhaps after the build is over thru some lipo -remove command or something.

I am scared to put #if DEBUG around everything including #Preview because it feels like some kind of violation of Apple's intent, and who knows what more harder-to-debug problem that will create later down the line.

This question is only applicable to Xcode 15 or greater. I will not tag this question as Xcode 15 because it is not meet that tag's guidelines.

0

There are 0 answers