I have a simple SwiftUI project, when I click the any list items, I want to route different SwiftUI. Below, I used arrays, and for every SwiftUI, I want to use [0], [1],..., but it throw error, I do not know why? Any idea?
ContentView:
Settings.swift:
NumberOneView:
NumberTwoView:
ContentView:
Code Block import SwiftUI struct ContentView: View { var body: some View { NavigationView { List(contacts) { contact in NavigationLink(destination: NumberOneView(contact: contact)) { ContactRow(contact: contact) } } .navigationBarTitle("Contacts") } .environment(\.colorScheme, .light) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct ContactRow: View { let contact: Settings var body: some View { HStack { Image(contact.imageName) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 20, height: 20) VStack(alignment: .leading) { Text(contact.name) .font(.system(size: 21, weight: .medium, design: .default)) } } } }
Settings.swift:
Code Block import Foundation import SwiftUI struct Settings: Identifiable { let imageName: String let name: String let id = UUID() } let contacts = [ Settings(imageName: "image1", name: "NumberOne"), Settings(imageName: "image2", name: "NumberTwo"), Settings(imageName: "image3", name: "NumberThree"), Settings(imageName: "image4", name: "NumberFour"), ]
NumberOneView:
Code Block import SwiftUI struct NumberOneView: View { let contact: Settings var body: some View { Text("hey") } } struct NumberOneView_Previews: PreviewProvider { static var previews: some View { NumberOneView(contact: contacts[0]) } }
NumberTwoView:
Code Block import SwiftUI struct NumberTwoView: View { let contact: Settings var body: some View { Text("hey") } } struct NumberTwoView_Previews: PreviewProvider { static var previews: some View { NumberTwoView(contact: contacts[1]) } }