SwiftUI PreviewProvider no longer surrounded with #if DEBUG #endif, what does this mean?

The release note here:

Resolved Issues

  • The #if / #endif compiler conditionals surrounding PreviewProvider types have been removed from SwiftUI templates. PreviewProviders aren’t properly removed from built products when archived. (51539802)

I don't quite understand what it mean? How does the PreViewProvider get strip out now?

I have some extra code only needed for preview, I should surround them with #if DEBUG / #endif, yes?

1 Like

Maybe it gets stripped out because it's dead code? Since nothing else in your program would be using it.

Maybe it gets stripped out because it's dead code?

Yes, But how? It was like this:

#if DEBUG
struct PreviewFoo: PreviewProvider {
....
}
#endif

So it's stripped if DEBUG is not define. But now, the generated code is just:

struct PreviewFoo: PreviewProvider {
....
}

So how does this gets strip out now?

That's dead code, because it's never actually used by your program (well, unless you are using it for some reason). Perhaps there was a bug that was preventing it from being optimised out, and now has been fixed. Obviously, I am speculating because I don't work on SwiftUI.

2 Likes

I know this code is stripped when not in DEBUG build. I want to know what's changed in the latest Xcode to do this now without having to surround them with #if DEBUG / #endif...

I know Preview is for DEBUG only. I want to know about how Xode now do the stripping out without the need to surround them in #if DEBUG / #endif.

Are you perhaps confused between dead code elimination and compiler directive?

Just for the record, that release note probably contains a typo, because its literal meaning makes no sense.

Presumably it meant to say this instead:

  • The #if/#endif compiler conditionals surrounding PreviewProvider types have been removed from SwiftUI templates. PreviewProviders [are now] properly removed from built products when archived[, even without the compiler conditionals]. (51539802)

This is the meaning assumed by @suyashsrijan and @Lantua’s explanations above.

1 Like

I just want to know why the latest generated dont have #if DEBUG / #endif. So from the comment below, the release note has a typo? I don’t understand it as it’s written.

I want to know how, with no #if DEBUG / #endif, the code is stripped in the end. Do you mean this is done with compiler dead code elimination? I want to know because I have extra code that’s only needed for SwiftUI Preview. Do I need to surround them with #if DEBUG / #endif or not?

1 Like

As I understand the text. Yes, it is done during optimization period, and no, you don't need to surround them (since Xcode doesn't surround them either).

2 Likes

I'm a little late, but I just had to make note of this because the confusion is all over the web and it's quite comical. Turns out, it was under "Resolved Issues" and the title of the resolved ticket was "PreviewProviders aren’t properly removed from built products when archived. (51539802)".

Yeah, it all makes sense now.

1 Like