UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

3      

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)
    }
}

3      

Thank you! I get the logic now.

2      

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.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.