TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day 37: Why am I creating both an expense Struct and class?

Forums > 100 Days of SwiftUI

I watched the videos twice already, and I am taking a break to redo iExpense and really metabolise how it works. What I really don't get is why we're creating 2 extra files, one with a struct and one with a class.

I understand the class is made to define a new type of variable, Expenses, but why do we need the ExpenseItem.swift file with a struct defining expenses, instead of having it all in the class?

Please explain to me as the newbie I am. No simplification will be too simple for me 😉

3      

From architecture point of view, meaning why they are created in different files, this is for convenience purpose. You can just put the class and the struct in ContentView file as well, and nothing will change. As your project grows it will become more difficult to search for necessary parts of the code etc. So it makes sense to separate those parts in different files so that you can navigate easily.

As for why we have Expenses class and ExpenseItem struct, think of it as: Your Expenses is like a LogBook for all your expenses your made. So this object will contain separate ExpenseItems that consist of some data (i.e. date, descprition, amount etc.).

Classes can conform to ObservableObject protocol that you can "observe", meaning if anything changes in class published property, it will notify View that uses that class and it will refresh data. Struct on the other hand is used as a blueprint model and does not provide such options as class.

Basically in this project Expenses Class is your LogBook object, and you have one logbook, so you can reference to it in your app. And ExpenseItem struct is single entry in your logbook but you will have many of them.

You can keep them all in one file, but I repeat myself, in real project as it will grow it becomes cumbersome to read, navigate etc., so it is more convenient to separate those pieces in different files.

4      

Thank you @ygeras ! That's exactly what I wanted to understand.

From architecture point of view, meaning why they are created in different files, this is for convenience purpose. You can just put the class and the struct in ContentView file as well, and nothing will change. As your project grows it will become more difficult to search for necessary parts of the code etc. So it makes sense to separate those parts in different files so that you can navigate easily.

This makes complete sense, since I have already started hating having to scroll up and down to find things.

Basically in this project Expenses Class is your LogBook object, and you have one logbook, so you can reference to it in your app. And ExpenseItem struct is single entry in your logbook but you will have many of them.

And this is exactly what I was trying to understand! So basically, I define what a single entry will look like with a struct, and reference it in a class as if the class was a binder and each entry (the struct) a sheet I can slot in, which is why it's an array. Thank you SO much for this!

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.