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

SOLVED: Pesky Previews - Moonshot

Forums > 100 Days of SwiftUI

I missed something in the way the preview code works (variables, parameters), and when I created a new View for the Moonshot challenges, I cannot get the preview to build. :

struct AstronautHorizList: View {
    var crew: [CrewMember]
     var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack {
                ForEach(crew, id: \.role) { crewMember in
                    NavigationLink {
                        AstronautView(astronaut: crewMember.astronaut)
                    } label: {
                        HStack {
                            Image(crewMember.astronaut.id)
                                .resizable()
                                .frame(width: 104, height: 72)
                                .clipShape(Circle())
                                .overlay(
                                    Circle()
                                        .strokeBorder(.white, lineWidth: 2)
                                )

                            VStack(alignment: .leading) {
                                Text(crewMember.astronaut.name)
                                    .foregroundColor(.red)
                                    .font(.headline)
                                Text(crewMember.role)
                                    .foregroundColor(.blue)
                            }
                        }
                        .padding(.horizontal)
                    }
                }
            }
        }
    }

}

struct AstronautHorizList_Previews: PreviewProvider {

    static var previews: some View {
        AstronautHorizList(crew: CrewMember)
            .preferredColorScheme(.dark)
    }
}

2      

Updated code, thanks

2      

I answered a similar question back in '21 regarding previews. I think the answer still holds up.

See -> Problems with Previews

In short, your application has ways to create data in one file (via JSON decoding), form that data into objects (array of CrewMembers), then pass copies of the array to other objects.

In a preview, you don't have this luxury.

Think of a preview as pulling a view OUT of your application and placing it on your desk. Now you don't have the other code. You don't have the JSON decoder. You don't have the code to create your astronaut arrays. How, then, will you get it to display itself?

What you need to do is provide it with fake data so it can use that fake data to display.

Your view needs an array of CrewMember. An array can have just one item in it. Can you think of a way to create ONE CrewMember object and stuff it into an array, the put that array into your Preview struct?

2      

Gerry observed:

I cannot get the preview to build

You didn't provide the error message. But it probably gives a great clue!

Add your error message to this thread, and give us your BEST interpretation of what you think it means.

We're available to help you interpret and understand it.

2      

Thanks for the clarification on preview data. I tried creating an array with one member of type CrewMember. I did it all on one statement rather than creating a Crewmember and putting it in an array because xcode complained the initializer could not be run before self was available. However, the code below now complains that

Instance member 'crew' cannot be used on type 'AstronautHorizList_Previews'

Does this mean that crew is not an array?

struct AstronautHorizList_Previews: PreviewProvider {

    let crew = [CrewMember(role: "Test Role", astronaut: Astronaut(id: "gerry", name: "gerry", description: "the coder"))]

    static var previews: some View {
        AstronautHorizList(crew: crew)
            .preferredColorScheme(.dark)
    }

}

2      

Without looking over you shoulder as you type, I am not getting a full picture. But here's some general advice: Let the compiler help you.

First, this line may cause confusion.

AstronautHorizList(crew: crew)  // crew? crew??

The compiler may give you a clue about a variable, but you look at the wrong one.

Instead, be verbose! Perhaps the error will highlight a different part of code.

Try:

struct AstronautHorizList_Previews: PreviewProvider {
    let firstTestAstronaut  = Astronaut(id: "gerry",     name: "Gerry", description: "Student")
    let secondTestAstronaut = Astronaut(id: "twoStraws", name: "Paul",  description: "Teacher")

    let missionCrew = [CrewMember(role: "Commander", astronaut: firstTestAstronaut),
                       CrewMember(role: "Pilot",     astronaut: secondTestAstronaut)]

    static var previews: some View {
        AstronautHorizList(crew: missionCrew)
    }
}

Furthermore:

Instance member 'crew' cannot be used on type 'AstronautHorizList_Previews'
Does this mean that crew is not an array?

If you hold your cursor over the variable missionCrew whilst holding the option key, Xcode will do its best to tell you want kind of object will be assigned to that variable.

In your case you should see:

    let arrayOfIntegers = [Int]()    //  <-- Hold the option key, until your cursor becomes a question mark: ?
                                     // Then click! Xcode will tell you the type 
                                     // of the object you've selected.

    // You'll get a dialog like this:
    Declaration
    let arrayOfIntegers: [Int]     // <-- Xcode tells you what arrayOfIntegers will be.

2      

You would need to make the array in your preview a static let property.

In the error message "Instance member crew cannot be used on type AstronautHorizList_Previews". It is basically telling you that you can't use an "Instance member" in the preview, and must use a "Static member" instead.

"Member" basically means a property or method of that struct, and "Instance Members" are those properties or methods that are not declared as static.

2      

Thanks @Obelix and @Fly0strich - That cleared up some important concepts for me and allowed me to continue working the course. I will need to refresh my understanding again soon of classes/members/instances and the way SwiftUI makes use of them. Great to have resources like you available to help - Thanks again

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.