|
I spend way too much hours on trying to get the last part of the challange to work : "For a bigger challenge, try splitting the expenses list into two sections: one for personal expenses, and one for business expenses. This is tricky for a few reasons, not least because it means being careful about how items are deleted!" I also searched a lot on the forum and think I have found an answer/ more help. There was an hint and the OP from that post said that helped him a lot. But I don't want to just copy and paste and then still don't understand. |
|
Hi, try to filter the array using the type property in the structure, then create an expense section (I suggest you to create a reusable view). Last think how to implement a function that deletes the correct expense. The function should take as argument the expenses array of the desired category. |
|
@suzanne is learning via frustration. This is a valid use of her time! But it's time for a hint.
Source of TruthTo answer the first part, consider the true list of expenses in your application. What is the absolute source of truth? You'll need to identify the source and make sure any changes you make (adding, deleting) are made to the source of truth. Do not try to update sublists, or temporary holding variables. Update your source of truth! PlaygroundRun this code in playgrounds. Notice how you have one and only one source of truth for students. But you can use array functions to extract portions of your data for other purposes. For example, maybe you just want to know who's in Hufflepuff.
|
|
@Obelix and @andreasara-dev Thankyou for your responses! I orderd myself to really stop for now since I have spend most of today on this topic and really need to drink my birthday beer for the day is over ;). But I will read your responses again tomorrow and with new energy and hope I will see if it will help me further. Otherwise I will ask some new questions |
|
Hi
Taken from Solutions The final challenge was to split the expenses list into two sections: one for personal expenses, and one for business expenses. I said this one was a little harder because you need to give some thought to how you divide up the two categories, but in my view the best place for this is computed properties in the That would mean starting by adding these two to the class:
That was the easy part. The trickier part is in Being able to find items by hand means telling Swift how it can check whether one item is equal to another, which means making ExpenseItem conform to the Equatable protocol. In its simplest form that means just adding Equatable to its declaration, like this:
When we do that we’re asking Swift to generate the Next we need to update the
We need to wrap that in two simpler methods that SwiftUI can call directly from
Let you work out how to use it |
|
@Obelix edit: with the delete fuction I was thinking about the deletefunction in the current project iExpense. |
|
The lightbulb moment for @suzanne!
Exactly! I love it when programmers work through the logic with gentle hints from this forum. Wonderful!
This is a great lesson for you. Try to design your screens and data models to support one Source of Truth™. Even though you may pass subsets of this data to other views, or to rows in a scroll view, or to a popup view, you need to update the Source of Truth, otherwise you'll have unintended side effects. Keep coding! |
|
@Obelix I'm also making this more complicated on my way (of course haha). Yesterday I already spotted https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-38-challenge-3-how-to-do-it/13348. and also https://www.hackingwithswift.com/forums/swiftui/delete-rows-in-sorted-list/20315. With person also busy with day 38, I see thet they have two sections and ForEach loops in the contentView. And I was wondering if that couldn't be done differently. I was thinking that is we get another group with costs for some reason, there wille be another section in the code with 99% the same lines of code. Since we have an constant with types, I was wondering if we can not use that to make one ForEach loop. Not completly sure how that should be working with secion headers. But somehow I am convinced that we shouldn't need to write the same section twice (or even more if there are more types added) |
|
Today spend anonther day on this project and lot of searches online. But it works! So happy me :). To be honest, I used the answers @NigelGee gave and did the last action myself to get things really working. I could have done that the first moment I looked at it today but still wanted to solve it myself and more important understand it. So that is why I still did many searches online and spend a lot of time. The thing I mentioned myself about having two sections, is something I will look into in a other moment. |
|
@suzanne finds the right helper:
Yes, @nigel is well known in this forum for providing great solutions. Well done on finishing this task. |
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.