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

Day 42 - Challenge 2 - Extract horizontal scroll view from Mission View - Trouble with the Preview

Forums > 100 Days of SwiftUI

Hi, so im having trouble with this part of the challenge. I got the first one (extract the rectangle to its own swiftui view) but this one im having trouble with the preview as it asks for a crew value.

After some tests right now i have this code on the preview

struct AstronautListView_Previews: PreviewProvider {
    struct CrewMember {
        let role: String
        let astronaut: Astronaut
    }

    static let missions: [Mission] = Bundle.main.decode("missions.json")
    let astronauts: [String:Astronaut] = Bundle.main.decode("astronauts.json")
    let crew = [CrewMember(role: "test", astronaut: Astronaut(id: "", name: "", description: ""))]
    static var previews: some View {

        AstronautListView(crew: crew)
    }
}

I also tried to do a real crew element selecting a mission and everything, but I always get the same errors:

Cannot convert value of type '[AstronautListView_Previews.CrewMember]' to expected argument type '[AstronautListView.CrewMember]' Instance member 'crew' cannot be used on type 'AstronautListView_Previews'

I dont know how to continue this part, I followed the tutorial part where he makes the crew array and try to implement it 1:1 in the preview code with no luck.

2      

Update:

So I got it working with a placeholder crew array, when im trying to make a real example one like in the other views I got another error:

With this code

struct AstronautListView_Previews: PreviewProvider {

    static let missions: [Mission] = Bundle.main.decode("missions.json")
    let astronauts: [String:Astronaut] = Bundle.main.decode("astronauts.json")

    static var previews: some View {
        var crew2 = missions[0].crew.map {member in
            if let astronaut = astronauts[member.name]{
                return CrewMember(role: member.role, astronaut: astronaut)
            } else {
                fatalError("Missin \(member.name)")
            }
        }
        let crew = [CrewMember(role: "test", astronaut: Astronaut(id: "", name: "", description: ""))]
        AstronautListView(crew: crew)
    }
}

In the line var crew2 = missions[0].crew.map {member in I get ther error "Generic parameter 'T' could not be inferred" pointing to the word "crew"

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.