BLACK FRIDAY: Save 50% on all books and bundles! >>

SOLVED: Move to next item in array

Forums > SwiftUI

I am trying to get to the next item in my custom struct in SwiftUI from the item detail screen; I'm trying to navigate to the next item in the list without going back. I use the below function, but I get out of index error each time I execute:

func nextItem() -> ItemDetailView {
    let currentIndex = userData.itemArray.firstIndex(of: item) ?? 0
    var nextIndex = currentIndex + 1
    nextIndex = userData.itemArray.indices.contains(nextIndex) ? nextIndex : 0
    return ItemDetailView(item: userData.items[nextIndex])
}

Any ideas?

   

   

Hi @bitigchi,

Had a play around with this and it depends on how you set up your array but if put a Int in it then pass that to the DetailView and have a reference to the Array, then you can just add one to the Int.

// Set up the Array that pulled from a JSON file
struct Astronaut: Codable, Identifiable {
    let id: Int
    let name: String
    let description: String
}
struct ContentView: View {
    let astronauts: [Astronaut] = Bundle.main.decode("astronauts.json") // <- Getting the Array
    var body: some View {
        NavigationView {
            List {
                ForEach(astronauts, id: \.id) { astronaut in
                    NavigationLink(destination: DetailView(astronautID: astronaut.id)) { // <- pass selected ID(Int) to DetailView
                        Text(astronaut.name)
                    }
                }
            }
            .navigationBarTitle("Main")
        }
    }
}
struct DetailView: View {
    let astronauts: [Astronaut] = Bundle.main.decode("astronauts.json") // <- Getting the Array again
    @State var astronautID: Astronaut.ID // <- Passed ID(Int) from List

    var body: some View {
        VStack {
            Text(astronauts[astronautID].name)
                .font(.largeTitle)
                .padding()
            Text(astronauts[astronautID].description)
                .padding()
            Spacer()
            HStack {
                Button(action: {
                    self.astronautID -= 1  // <- goto previous item in Array
                }) {
                    Image(systemName: "chevron.left")
                    Text("Previous")
                }.disabled(astronautID <= 0) // <- should stop going out of range
                Spacer()
                Button(action: {
                    self.astronautID += 1 // <- goto Next item in Array
                }) {
                    Text("Next")
                    Image(systemName: "chevron.right")
                }.disabled(astronautID >= astronauts.count) // <- should stop going out of range
            }
            .padding()
        }
        .navigationBarTitle("Detail", displayMode: .inline)
    }
}

1      

Thank you! I get the logic now.

   

You could change let astronauts: [Astronaut] = Bundle.main.decode("astronauts.json") to var astronauts: [Astronaut] and then in ContentView change NavigationLink(destination: DetailView(astronauts: astronauts, astronautID: astronaut.id) this will save you calling the JSON file twice.

But the principal is the same.

   

Save 50% in my Black Friday sale.

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.