0

Why is "init" printed four times?

import SwiftUI 

struct HomeView: View {

    let items: [String] = [“aaa”, “bbb”]
    
    var body: some View {

        NavigationView {

            ScrollView {

                ForEach(colors, id: \.self) { color in

                            VStack {

                                NavigationLink(destination: StudentView()) {
                                        Image(systemName: "trash.fill")
                                            .resizable()
                                            .frame(width: 100, height: 100)

                                }

                            }

                }

             }


        }
    }
}

================================================

import SwiftUI

struct StudentView: View {
        
    init() {
        print("init")
    }
    

    var body: some View {
            
            ScrollView {

                
            }      
    }
}
KevinP
  • 2,562
  • 1
  • 14
  • 27
  • 1
    Please spend some time on properly formatting your code – Joakim Danielson Jan 28 '21 at 12:44
  • 1
    *Why is "init" printed four times?* - because SwiftUI rendering engine creates this view struct four times. It can be any times - it is struct. You should not rely on this. – Asperi Jan 28 '21 at 12:48
  • Have a look at this [SO Answer](https://stackoverflow.com/a/61234030/4080925) it will solve your issue with the destination view loading. When `HomeView` loads. – MwcsMac Jan 28 '21 at 21:29

0 Answers0