Error in SwiftUI

Hello, I'm writing on this forum because I am currently coding my first app, and I have an error that appears, and after multiple attempts, I can't seem to solve the problem. I don't know if it prevents the SwiftUI code Preview window from appearing, but well...

Here is the error in question:

'init(destination:tag:selection🏷️ )' was deprecated in iOS 16.0: use NavigationLink(value🏷️ ), or navigationDestination(isPresented:destination:), inside a NavigationStack or NavigationSplitView

And here is my code:

import SwiftUI import SwiftData

struct ContentView: View { @State private var selection: String? = nil

var body: some View {
    NavigationView {
        ZStack {
            Image("BACKGROUND")
                .resizable()
                .edgesIgnoringSafeArea(.all)
            
            VStack {
                Spacer()
                
                VStack {
                    NavigationLink(
                        destination: GameView(),
                        tag: "jouer",
                        selection: $selection
                    ) {
                        EmptyView()
                    }
                    
                    Button("Jouer") {
                        selection = "jouer"
                    }
                    .padding()
                    .foregroundColor(.black)
                    .background(Color.white)
                    .cornerRadius(10)
                    .padding(10) // Ajoutez une marge supplémentaire si nécessaire
                    
                    Button("Chapitre") {
                        // Code à exécuter lorsqu'on appuie sur le bouton "Chapitre"
                    }
                    .padding()
                    .foregroundColor(.white)
                    .background(Color.blue)
                    .cornerRadius(10)
                }
                
                Spacer().frame(height: 20)
                
                HStack {
                    Spacer()
                    
                    Image(systemName: "gearshape")
                        .font(.system(size: 12))
                        .foregroundColor(.white)
                        .onTapGesture {
                            // Code à exécuter lorsqu'on appuie sur le bouton d'engrenage
                        }
                }
            }
        }
    }
}

}

Thank you very much to anyone who can help me :)

Answered by Claude31 in 774535022

No, it works as is and works in Preview as well:

You may have 2 problems:

  • not calling Preview
  • gearbox is white on white.

I changed as follows:

struct ContentView: View {
    @State private var selection: String? = nil
    
    var body: some View {
        NavigationView {
            ZStack {
                Image("BACKGROUND")
                    .resizable()
                    .edgesIgnoringSafeArea(.all)
                
                VStack {
                    Spacer()
                    
                    VStack {
                        NavigationLink(
                            destination: GameView(),
                            tag: "jouer",
                            selection: $selection
                        ) {
                            EmptyView()
                        }
                        
                        Button("Jouer") {
                            selection = "jouer"
                        }
                        .padding()
                        .foregroundColor(.black)
                        .background(Color.white)
                        .cornerRadius(10)
                        .padding(10) // Ajoutez une marge supplémentaire si nécessaire
                        
                        Button("Chapitre") {
                            // Code à exécuter lorsqu'on appuie sur le bouton "Chapitre"
                        }
                        .padding()
                        .foregroundColor(.white)
                        .background(Color.blue)
                        .cornerRadius(10)
                    }
                    
                    Spacer().frame(height: 20)
                    
                    HStack {
                        Spacer()
                        
                        Image(systemName: "gearshape")
                            .font(.system(size: 12))
                            .foregroundColor(.blue)
                            .onTapGesture {
                                print("Tapped")
                                // Code à exécuter lorsqu'on appuie sur le bouton d'engrenage
                            }
                        Spacer()
                    }
                }
            }
        }
    }
}

#Preview {  // <<-- ADD THIS
    ContentView()
}

I don't understand what selection does in your code, nor tag.

You can simply replace by

                        NavigationLink(destination: GameView() )
                        {
                                Text("jouer")
                        }

To silence the warning, or use a NavigationStack

Accepted Answer

No, it works as is and works in Preview as well:

You may have 2 problems:

  • not calling Preview
  • gearbox is white on white.

I changed as follows:

struct ContentView: View {
    @State private var selection: String? = nil
    
    var body: some View {
        NavigationView {
            ZStack {
                Image("BACKGROUND")
                    .resizable()
                    .edgesIgnoringSafeArea(.all)
                
                VStack {
                    Spacer()
                    
                    VStack {
                        NavigationLink(
                            destination: GameView(),
                            tag: "jouer",
                            selection: $selection
                        ) {
                            EmptyView()
                        }
                        
                        Button("Jouer") {
                            selection = "jouer"
                        }
                        .padding()
                        .foregroundColor(.black)
                        .background(Color.white)
                        .cornerRadius(10)
                        .padding(10) // Ajoutez une marge supplémentaire si nécessaire
                        
                        Button("Chapitre") {
                            // Code à exécuter lorsqu'on appuie sur le bouton "Chapitre"
                        }
                        .padding()
                        .foregroundColor(.white)
                        .background(Color.blue)
                        .cornerRadius(10)
                    }
                    
                    Spacer().frame(height: 20)
                    
                    HStack {
                        Spacer()
                        
                        Image(systemName: "gearshape")
                            .font(.system(size: 12))
                            .foregroundColor(.blue)
                            .onTapGesture {
                                print("Tapped")
                                // Code à exécuter lorsqu'on appuie sur le bouton d'engrenage
                            }
                        Spacer()
                    }
                }
            }
        }
    }
}

#Preview {  // <<-- ADD THIS
    ContentView()
}

I don't understand what selection does in your code, nor tag.

You can simply replace by

                        NavigationLink(destination: GameView() )
                        {
                                Text("jouer")
                        }

To silence the warning, or use a NavigationStack

Thank you very much for your help! I'm trying to learn on my own without formal training, and I sometimes use the assistance of an AI to learn, hence the potential errors as well. If you have any advice for learning, guides, or videos, I would greatly appreciate it.

Thanks !

Error in SwiftUI
 
 
Q