|
I played around with it a bit, but couldn't get it. To make sure, are we supposed to present in ContentView 2 separate sections of Personal and Business? I tried and if and else condition, but I'm not sure where to put it. I tried wrap it before ForEach and after, neither works. ContentView Code:
AddView Code:
|
|
I found this one to be difficult when I first came across it. Here's a hint: try adding the following computed property to your Expense class and see if you can figure it out from there:
Avoid trying to use an if statement here. Think about iterating over the above items in a ForEach like you did for expenses.items. I do think this challenge is really hard. You've met .filter before but it was a long time ago in the lesson on complex data types. |
|
I found deleting the correct items from Expenses is pretty challenging. Hang in there and ask if you find yourself completely stuck on it. |
|
@vtabmow, thank you very much for your hint. Solved it within minutes after seeing your comment haha. @ty-n-42, I'm still working on that part. I think for this part, it really depends on how we write the setter for the computed properties --- the ones that vtabmow listed above. This is tricky. |
|
I cannot figure out the last part, how to delete items properly. This is what I've got so far: I'm having trouble writing out "ExpenseItem" in the setter for personalItems. I think this is the key. Anybody chime in? Please ignore the line written for businessItems...
|
|
Great! You’re halfway there. You are correct. This is tricky. You will use what you’re learning in this particular challenge a lot, so keep at it. Your original removeItems method receives an IndexSet of items to be deleted from expenses.items. Now, you probably have two sections in your ContentView where you are implementing a ForEach to list out personalItems and businessItems. So, each one of those sections will:
|
|
vtabmow, thank you for your help. I still couldn't get it. Right now, for the method, I have this:
I believe the for loop works. If it doesn't, I will try ForEach The trouble is, I still don't know how to set the personalItems property in expenses. I'm having trouble writing that set { }. If I delete the line, the method gives an error that says personalItems is a get-only property. How do I set it to be something that conforms to a "whatever's left after you delete"?
full code:
|
|
So you want something like this:
|
|
That solves it ! Didn't think about removing items from expenses.items, which should have been the one since it is where information is stored, not personalItems or businessItems. |
|
Yeah I'm glad I didnt waste too much time on this one lol Thanks for the solution. I'll definietly learn from it. |
|
Why is the solution from @vtabmow working? What does the $0.id mean on the context? Is the closure running an implicit for loop over expenses.items on the background? why? I cannot get my head around why is the solution working. Because it does! |
|
It is not running an implict loop, but an explict loop defined by
This chooses a single element The Here are a couple of examples of using $0 and $1 which hopefully will help you understand better. |
|
I did manage to do this without adding too much. I was surprised how easy it was, at last. Or have I overlooked something crucial? The "All items"-Section is just for testing purposes, if the wanted items are deleted correctly.
|
|
@Ollikely I really like your very simple and, for me, clear solution - good job. For me as a beginner it was very helpful and i have learned a lot from your solution - Thanks! |
|
@olliklee I really like your approach, as it's clean and descriptive. But I am not pretty sure if it's really working. When iterating like this:
and later:
the Cheers Matthias |
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.