Use alert with toolbar cause preview crash

I'm new to iOS dev and SwiftUI. And I have some very simple code, but it keeps crash in live preview, I don't know why... It runs completely normal in simulator though.

import SwiftUI

struct MainView: View {
  @State var showingAlert = false

  var body: some View {
    NavigationView {
      Button( "Tap me") {
        showingAlert = true
      }
      .toolbar {
        Label("bell", systemImage: "bell")
      }
    }
    .alert("Alert", isPresented: $showingAlert) {
      Button("OK", role: .destructive) { }
    }
  }
}

struct MainView_Previews: PreviewProvider {
  static var previews: some View {
    MainView()
  }
}

But when I change the toolbar part a little, it turns to be ok.

       .toolbar {
//        Label("bell", systemImage: "bell")
        Text("bell")
      }

My Xcode Version 14.2 (14C18)

The use of NavigationView has been deprecated. Apple states:

If your app has a minimum deployment target of iOS 16, iPadOS 16, macOS 13, tvOS 16, or watchOS 9, or later, transition away from using NavigationView. In its place, use NavigationStack and NavigationSplitView instances.

Try your code with NavigationStack instead.

import SwiftUI

struct MainView: View {
  @State var showingAlert = false

  var body: some View {
    NavigationStack {
       
            Button( "Tap me") {
              showingAlert = true
            }
            .navigationTitle("Title")
            .toolbar {
                Label("bell", systemImage: "bell")
            }

    }
    .alert("Alert", isPresented: $showingAlert) {
      Button("OK", role: .destructive) { }
    }
  }
}

struct MainView_Previews: PreviewProvider {
  static var previews: some View {
     
          MainView()
     
  }
}

The preview section should be modified as follows in the above post.

static var previews: some View {

      NavigationView {
          MainView()
      } 

  }
}

After much trial and error with this same problem, I found that cleaning the build folder (cmd + shift + K) makes this issue disappear.

Use alert with toolbar cause preview crash
 
 
Q