|
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. :
|
|
|
|
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 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 |
|
Gerry observed:
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. |
|
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
Does this mean that crew is not an array?
|
|
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.
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:
Furthermore:
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:
|
|
You would need to make the array in your preview a In the error message "Instance member "Member" basically means a property or method of that struct, and "Instance Members" are those properties or methods that are not declared as |
|
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 |
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!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.