UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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)

2      

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.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.