|
For example, this works: GridView is more or less the same thing as ListView. They are two separate SwiftUI View files created. What's interesting is I can only put ScrollView above LazyVGrid since List automatically comes with a scroll function. If I wrap List inside ScrollView, it doesn't appear in the UI. I did not pass in the astronaut and mission parameters like Paul said in the hints. Why would you want to create two separate structs and pass in parameters? If seems like extra trouble to go through, or am I missing something here? ContentView
ListView
|
|
I think it is worthwhile keeping variables within a struct/class that makes most sense to "own" the variable. It's definitely extra code and doesn't seem worth it for small apps; but as the code grows and the complexity increases it can make a big difference with trying to fix problems and understand the impact of making changes. Organising functionality and data so they are "inside" other code and data is used all over the place in almost all modern programming languages. When we create functions for a struct/class we are helping ourselves understand our code by grouping things together. When we put variables inside a struct/class we are doing the same thing. When we organise our code inside structs/classes/enums we get the added benefit of being able to say what other code can see/use that code: the private keyword is an example of telling Swift that only the struct/class/enum that the code is contained in can see/use it. Once again, this is mostly beneficial when code grows and is complex. IMHO even though this way of organising code is really only beneficial when code gets big I think it is a good idea to get in the habit of this kind of code structure - after all, don't we all want to be skilled enough to someday create big, complex code |
SPONSORED An iOS conference hosted in Buenos Aires, Argentina – join us for the third edition from November 29th to December 1st!
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.