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

Moonshot Exercise

Forums > 100 Days of SwiftUI

Did anyone come up for a solution for exercise 3 of the Moonshot project. Easy to add a button to toggle but how to actually show the full crew names?

2      

Hey! Made a pretty rough/iffy solution but it works. I ended up just using the code mission names of each astornaut. If you want to use the proper astornaut names I think it could be a good idea in init() to create a tuple array, essentially matching the crew code names to their respective full astronaut names. You can store this as a field/instance variable then - this tuple array - and use it to print out their full names.

Keep in mind that the UI is really bad, but here's what I did. Take what you will from it.

import SwiftUI

struct ContentView: View {
    let astronauts : [Astronaut] = Bundle.main.decode("astronauts.json")
    let missions : [Mission] = Bundle.main.decode("missions.json")
    @State private var toggle : Bool = false
    var body: some View {
        NavigationView{
            List(missions) { mission in //no need for id because identifiable I think. Automatic placement to the left!
                //For each mission we are instantiating the view!
                NavigationLink(destination: MissionView(mission: mission, astronauts: self.astronauts)) {
                    Image(mission.imageName).resizable().scaledToFit().frame(width: 44, height: 44)
                VStack(alignment: .leading) {
                    Text("\(mission.displayName)").font(.headline)
                    if (self.toggle) {
                    Text(mission.formattedLaunchDate)
            }
                    else {
                        ForEach(mission.crew, id: \.role) { crew in
                            Text(crew.name)
                        }
                        //array of tuples.
                    }
            }
            }.navigationBarTitle("Moonshot").navigationBarItems(leading: Button("Toggle") {
                self.toggle.toggle()
            })
        }
    }
}
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.