|
Hi, I'm very new to Swift and SwiftUI, so I'm probably missing something obvious. I have a list of files and for each file's type I have a different View object. How do I set up my This is what I currently have:
As I add more and more types, I can see that this is going to get very tedious. Is there a better way? |
|
I see what you mean, and I'll be very interested in seeing what anyone else may suggest. My first thought is that you might stick the appropriate icon into the file type enumeration, and then make a more generic FileView:
Then, your body looks like:
|
|
Thanks. However, the |
|
I tend to keep my functions, methods, and bodies in SwiftUI fairly small. After 10-20 lines I look to refactor. The exception is a switch statement, which you can't really break up that easily. I think that is ok as it is, but as you said it might grow. However you could get away with some type erasing to AnyView . This wont compile but this is an idea:
Actually you could probably even use assoicated values in enums there, but I never got comfortable with that.
This is moving the swtich, not eliminating it, but it's neater. |
|
Without seeing the code for the various different But the approach described by @eoinnorris is on the right track. Since
|
|
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.