Different Build Schemes -> Error: -Onone Swift optimization level to use previews

I have a sample SwiftUI iOS app. As shown in the screenshot below, my project has three configurations: Debug, MyDebug, Release.

If I select the Debug or MyDebug scheme, I get a preview. But if I select the Release scheme, I get an error that says the following.

”***.app” needs -Onone Swift optimization level to use previews (current setting is -O)

, where *** is the app name.

It probably has nothing to do with the Preview error, but the Info.plist has a dictionary such that the key name is devMode, and the value is $(DEVMODE). And I have a user-defined setting as shown below.

My ContentView has the following.

import SwiftUI

struct ContentView: View {
    @State private var state: String = ""
    
    var body: some View {
        VStack {
            Text("Hello, world!: \(state)")
        }
        .onAppear {
            if let devMode = Bundle.main.object(forInfoDictionaryKey: "devMode") as? String {
                print("Development mode: \(devMode)")
                state = devMode
            }
            if let path = Bundle.main.path(forResource: "Info", ofType: "plist") {
                if let dict = NSDictionary(contentsOfFile: path) {
                    print("**** \(dict)")
                }
            }
            #if DEBUG
                print("Debug")
            #elseif MYDEBUG
                print("MyDebug")
            #else
                print("Que?")
            #endif
        }
    }
}

#Preview {
    ContentView()
}

So my question is how I get the preview for all three build schemes? Muchos thankos.

Answered by DTS Engineer in 837294022

Hi @Tomato,

This error is informing you that Previews cannot work with optimizations turned on. Release build uses -0 in the Swift compiler and other optimizations, like dead code stripping, in the linker. These are incompatible while running under Previews. You can only preview in a configuration like Debug that does not optimize the build.

Cheers,

Paris X Pinkney |  WWDR | DTS Engineer

Hi @Tomato,

I need more information from the previews diagnostics. Please follow the directions in the following post and reply here with the Feedback ID, once submitted, and we'll look into this.

Gathering Required Information for Troubleshooting Xcode Previews or Swift Previews Issues

https://developer.apple.com/forums/thread/775027

Cheers,

Paris X Pinkney |  WWDR | DTS Engineer

Hello, Paris and DTS.

My sample iOS app doesn't crash if I run it with a simulator or an actual device although the preview canvas shows an error for a selective build scheme. So I am afraid that I have nothing to report to you. I have installed the said profile as shown below.

It's hard to locate the one that I need to look for since the list has several dozen items. Yet, based on Today's date, I don't find one.

So what's next?

Hi @Tomato,

You wrote:

[...] It's hard to locate the one that I need to look for since the list has several dozen items. Yet, based on Today's date, I don't find one. [...] So what's next?

On iOS, the sysdiagnose file begins with "sysdiagnose". However, since the issue you're reporting occurs within Previews, we would like the sysdiagnose from your Mac or Xcode. Please see the instructions below:

Profiles and Logs

https://developer.apple.com/bug-reporting/profiles-and-logs/?name=sysdiagnose&platform=macos

Once you have the requested information, please attach it to your report via Feedback Assistant and reply here with the Feedback ID so I can escalate to the Previews engineering team.

Best,

Paris X Pinkney |  WWDR | DTS Engineer

Accepted Answer

Hi @Tomato,

This error is informing you that Previews cannot work with optimizations turned on. Release build uses -0 in the Swift compiler and other optimizations, like dead code stripping, in the linker. These are incompatible while running under Previews. You can only preview in a configuration like Debug that does not optimize the build.

Cheers,

Paris X Pinkney |  WWDR | DTS Engineer

I have seen somebody else's project where the Preview guy works under the Release build mode. I now presume that this Release mode is just a code of the original Debug mode.

Different Build Schemes -> Error: -Onone Swift optimization level to use previews
 
 
Q