WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Xcode cannot compile if there are too many modifiers

Forums > SwiftUI

I have noticed that Xcode fails to compile with

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

You can execute the below demo project to recreate the error. Once you uncomment confirmationDialog entries 11+ it fails to compile.

//
//  ContentView.swift
//  confDialogTest
//
//  Created by Max on 13.06.22.
//

import SwiftUI

struct ContentView: View {
    @State private var showingConf1 = false
    @State private var showingConf2 = false
    @State private var showingConf3 = false
    @State private var showingConf4 = false
    @State private var showingConf5 = false

    var body: some View {
        Button(role: .destructive) {

            self.showingConf1.toggle()
        } label: {
            Label("Conf 1", systemImage: "trash.fill")
        }
        .padding()
        Button(role: .destructive) {

            self.showingConf2.toggle()
        } label: {
            Label("Conf 2", systemImage: "trash.fill")
        }
        .padding()
        Button(role: .destructive) {

            self.showingConf3.toggle()
        } label: {
            Label("Conf 3", systemImage: "trash.fill")
        }
        .padding()
        Button(role: .destructive) {

            self.showingConf4.toggle()
        } label: {
            Label("Conf 4", systemImage: "trash.fill")
        }
        .padding()

        Button(role: .destructive) {

            self.showingConf5.toggle()
        } label: {
            Label("Conf 5", systemImage: "trash.fill")
        }
//        .padding()
        .confirmationDialog("Select location", isPresented: $showingConf1, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf2, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf3, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf4, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
            Text("Conf")
        }
        //next group is dialogs 6-10
        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
            Text("Conf")
        }
        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
            Text("Conf")
        }
        //next group is dialogs 11-15
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
        //next group is dialogs 16-20
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
//        .confirmationDialog("Select location", isPresented: $showingConf5, titleVisibility: .visible) {
//            Text("Conf")
//        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Whiel it is easy to fix here if I just move some confirmationDialogs to another Button view, I have the same problem in another problem where I have to add many modifiers to a NavigationLink in a List View and there I cannot move it around ... :/

Any ideas what to do here?

   

The reason the modifiers stop at 10 is the same as for views in general - explanation.

You could try writing an extension, so that you can group your modifiers together in some fashion. I have added a parameter in the function, just to show that you can do this.

extension NavigationLink {

    func myNavLinkExtension(bright: Bool) -> some View {
        self.foregroundColor(Color (bright ? .orange : .gray))
            .font(.largeTitle)
    }
}

then you would use it as a normal modifier

.myNavLinkExtension(bright: false)

   

Think I found the root cause of the issue. In my other project with the same problem, I have a structure like

Zstack{
  List{
    ForEach(sectionArray){
      Section{
        ForEach(array){
          NavigationLink{
            RowView
          }
          .confirmationDialog()
        }
      }
    }
  }
}

so I was attaching the confirmationDialog to every single NavigationLink and that seems to have killed it. Once I rearranged it to

Zstack{
  List{
    ForEach(sectionArray){
      Section{
        ForEach(array){
          NavigationLink{
            RowView
          }
        }
      }
    }
  }
}
.confirmationDialog()

the problem was gone.

   

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.