问题
The preprocessor macro's are pretty commonly seen in the SwiftUI official tutorials/videos, e.g.:
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Are those needed? The compiler can surely see that the struct
isn't used internally and omit the whole struct
since the access modifier is implicit internal
right? I think that everything that conforms to PreviewProvider
can be removed away, but maybe that isn't the case for every conforming object, but if it isn't used, why does Apple decides to include the preprocessor macro's?
I tried to run it in release mode and locate the compiled class in the derived data folder, but I don't understand anything about it (.o file). Can anyone confirm if we really need to include the macros to omit unused code (the ContentView_Previews type doesn't get used anywhere in the code expect for previewing which isn't used in the release build anyway) in the release build?
回答1:
NOTE: To be extra clear, you DO NOT need to wrap your preview providers in
#if DEBUG
conditionals. They are removed from your production build.
I'm a little late, but I just had to make note of this because the confusion is all over the web and it turns out to be quite comical. The release note 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.
The still The proof
Annnd Just in case you think they may have changed it later..... more proof
(I'm thorough... maybe too much so)
回答2:
It seems to be removed from the 11 GM Seed. The GM seed release notes say (under 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)"
Not sure if this means the preview providers aren't currently removed or they fixed the issue and now remove them. I suppose if the template code removes the #if it means Apple believes it is no longer necessary.
回答3:
Xcode 11.2 and higher do not need the #if DEBUG so, you would do the following and remove the #if DEBUG around the PreviewProvider
struct ContentView: View {
var body: some View {
NavigationView {
List {
Text("First Row")
Text("First Row")
Text("First Row")
Text("First Row")
}.navigationBarTitle(Text("Dynamic List"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
回答4:
You do not need to delete that. DEBUG is a custom flag that is added automatically to Your Target -> Build Settings -> Active compilation Conditions
. This flag is exist only for Debug configuration, for release build you will use Release configuration that doesn't have this flag.
If the flag doesn't exist - the code will be omitted
Read more here in section "The DEBUG preprocessor macro"
来源:https://stackoverflow.com/questions/56485562/are-the-if-debug-statements-really-needed-for-previews-in-swiftui-to-remove-it