|
Hi all, I am curious to know how you would translate my challenge into code - I am a (very) new developer so please have patience with me :) Currently, I am working on a small tool to support product managers in estimating the cost of their products, with the following logic:
I am currently working with the two classes Product, Component, and Raw material. However, I have difficulty in understanding how to make a component from other components. Could you help kick-start my mental challenge to get past my brain-block? :) Thank you. |
|
@blokhus has a brainblok:
From some of your past posts, it looks like you've worked your way through the 100 Days of SwiftUI course? Data ModelingYou describe a classic parent-child relationship in your product-component-material use case. That is, a product is made from one or more components, your components are made from one or more materials. In a Swift data model you'd represent the product as a class with parameters that describe a single product.
Note the contents of the LunchBasket may be empty! This is just a template. But I think you can figure this out? If you need a refresher, please review the Moonshot project. It implements one-to-many relationships with astronauts. One mission has three astronauts. Review the Mission struct to see how that was implemented. Components made from Material, or other ComponentsThis is tricky. What do components and materials have in common? Think about that and then review @twoStraw's lessons on class inheritance. Perhaps you can have both the components and the material inherit from a common class? They may implement some common parameters, perhaps some required methods. The key difference, of course, is that the Components class may consist of other Components! Then consider how your LunchBasket struct might implement an array of the parent class. I think there were some exercises with cats and dogs inheriting from a common Animal class? Go back to your Checkpoint 7 code in Playgrounds. Can you create an array containing Persian, Lion, Corgi, and Poodle? They are all subclasses of Animal. See -> Cats and Dogs Keep Coding!Please return here and share you Product, Component, and Material models with us! Also, keep asking questions. |
|
Paste this simple example into Playgrounds and noodle over the logic.
Keep CodingAs a homework assignement, please add tomatoes to the sandwich. For extra credit, add another complex food item to the sandwich. Let us know how you solved your problem. Share your knowledge. |
SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls β free of charge.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.