Using Color() in background modifier in SwiftUI does not work with Xcode 15.3

I had this code working properly in Xcode 17.0.1, with macOS target as 12.4

struct SyntheseView: View {
    @AppStorage("isDarkMode") var isDarkMode = false

       // more declarations ………

    var body: some View {
        
        VStack {   
            ZStack {
                // code …………………
            }
            .frame(width: 500, height: 300)
            .background(isDarkMode ? Color(red: 0, green: 0, blue: 120/255) : Color(red: 1, green: 1, blue: 186/255))
        }   // VStack

In Xcode 15.3, I get the error The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

If I comment out the background modifier, no issue.

I converted all parameters in Color to Double without success

If I replace by a fixed color as

.background(.blue)

No error.

Even the simple

            .background(Color(red: 0.0, green: 0.0, blue: 0.5))

causes the error.

But:

.background(isDarkMode ? .blue : .red)

does work.

I tried to define the Colors as static color extensions to no avail.

Another bizarre point is that a similar pattern works OK for another View.

Did something change in Xcode 17.3 ?

Answered by Claude31 in 782547022

SwiftUI error management is really terrible… Specially for this "too complex" error.

Removing the .background instruction did solve it… but the error was elsewhere in a Text() expression, made of strings + formatted strings from a computed var.

I modified it and everything works again OK.

That does not explain the difference with Xcode 15.0.1.

Accepted Answer

SwiftUI error management is really terrible… Specially for this "too complex" error.

Removing the .background instruction did solve it… but the error was elsewhere in a Text() expression, made of strings + formatted strings from a computed var.

I modified it and everything works again OK.

That does not explain the difference with Xcode 15.0.1.

Using Color() in background modifier in SwiftUI does not work with Xcode 15.3
 
 
Q