|
I spent the better part of last year learning Swift by building the backend code for my app, but now I'm at the point where I need to start designing the interface, and I figured I would try to learn SwiftUI at the same time. However, I've gotten to the point in my learning journey where the "out of the box" solutions most examples in tutorials demonstrate aren't quite applicable to what I'm trying to do, but at the same time, I'm not yet expert enough to be able to understand what the more complicated examples I can examine in various open-source projects are doing. My backend builds a database of
I'm wrapping the My individual types are classes, so I know I need to wrap them using
I don't actually get any errors when |
|
I'm not sure if what you're doing is wrong, but the way I'd do it would be to have some object encapsulate the array. Something like:
This way, any code that updates the list of books (filtering it, adding new items, removing items) would call methods on the |
|
Okay. Thank you. I had considered using that approach for my individual classes, instead of having the classes themselves conform to Question: Right now I've got my database as a whole wrapped like this to use as an
Would it be better to instead make the arrays within the database
And then add the method you suggested for replacing the arrays? |
|
Good question, but unfortunately we're running right up against the limits of my experience. My gut tells me that this is one of those decisions that really comes down to how hard it will be to maintain/modify in the future. The one insight I might offer is that so far, I've had better luck in Swift with shallow data structures than with really deep ones. |
|
Okay, thank you. With my far from solid-grasp of how Right now, as shown above, I have my JSON database wrapped as an Part of me wants to think that would mean that whenever a book (or author, or series) is added or removed from one of those arrays in the database, or a particular record is edited (new title, new authors, whatever) then, with the way my data model is set up, that change would happen to the database, and therefore any view watching the published database as an And the other part of me thinks there's no way it can be that easy and that I probably need, like you recommended, a So it would look more like this:
Yeah. I have no idea if I'm oversimplifying or overcomplicating things. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.