I'm seeing some weird behavior with conditional compilation when I use a build configuration other than "Debug" or "Release", and I'm wondering if I'm doing something wrong or if this is an Xcode bug.
The setup
Xcode version: 15.3
I have a simple SwiftUI view that takes in a model and displays an attribute of the model.
struct ContentView: View {
let model: Model
var body: some View {
VStack {
Text("Name: \(model.name)")
}
.padding()
}
}
In the model file, I have the struct definition, but also an extension that defines some sample data for use in SwiftUI previews:
struct Model {
let id: String
let name: String
}
#if DEBUG
extension Model {
static let example = Model(
id: "50fef362-f53d-4ded-9168-b887ff62e59d",
name: "John Doe"
)
}
#endif
And finally, I have a preview provider that uses this sample data:
#Preview {
ContentView(model: Model.example)
}
Normal behavior
With the default "Debug" build configuration, this works just fine. There are no compilation errors, and the preview renders as expected.
The issue
Start off by changing the name of the build configuration to, e.g. "DebugDev"
Now I start seeing seeing a mis-match between what happens and what Xcode tells me is happening.
The code still compiles and runs, and the SwiftUI preview still works, but Xcode is displaying things as if it doesn't compile.
Xcode formats the conditionally compiled code with a lower opacity color as if DEBUG isn't defined:
In the view file, I now see a compilation error when referencing the conditionally-compiled code:
Autocomplete doesn't work when trying to reference the conditionally compiled code
Additional details/observations
I have confirmed that the DEBUG active compilation condition is still defined in the build settings. All I've done is change the name of the build config
I was initially thinking this might have something to do with the fact that this appears in a preview provider, since those are treated a bit differently, but the same thing happens if I reference the conditionally compiled code directly in the view class
Another theory: since I was referencing conditionally compiled code from code that wasn't conditionally compiled, maybe Xcode was trying to tell me that wasn't valid. So, I tried placing the code that calls the conditionally compiled code (in this case, the view class) inside an #if DEBUG. This does get rid of the displayed compilation error, but auto-complete still doesn't work, and the whole class is displayed with the lower-opacity font.
Help?
I feel like I must be missing something. The only alternative I can think of is that Xcode has some logic hard-coded with the default "Debug" build config, and that would be...just silly.