|
I am having trouble printing the balance of a savings account for each future year. I am working within the framework of code which is functional in calculating deposits into a savings account each year.
The above code is functional but when I add in the following function below func yearlySavings, the build fails without an error (debugger is empty):
|
|
Just to clarify, when you say cumulative savings, you want to end up with a result like this:
Is that correct? |
|
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! |
|
Here's one way you could do it:
There are other ways, but I find this to be probably the easiest because you don't have to track indices in multiple arrays and such. A problem with this approach is that you iterate through the array many times, albeit not all of the iterations have the same length. Probably not too big a deal given that you wouldn't expect an employee to have more than a few dozen years of work experience, but still something to consider. An interesting way to do it but one I find kind of hard to read and reason about at first glance is this:
|
|
@roosterboy Thank you for your help! I've pressed forward onto the next phase which includes calculating the compound interest of roth 401k investments. Here is the general formula for 401k growth:
I've applied principles from the yearlySavings function to calculate an employee's regular deposit into his 401k:
It is after this that I am getting stuck. Here is my attempt to use the calc401k formula to calculate a yearly 401k balance:
The error is: "Value of type 'Department' has no member 'depositsToRoth'" One inherient problem is that I'm not accounting for change in principle year-to-year: the principle for year two is the balance for year one (and so on). |
|
A few issues I can see right away...
The purpose of a language like Swift is so that we don't have to write everything in assembly code. As such, it should be readable to humans. Don't be afraid to give your variables, parameters, etc actual names like
What is the purpose of this line? You set a Even if you do need it, Also, your
Here you are passing in the result of a call to Now look at the declaration for the
What type is the So take those comments into consideration and give it another whirl, see what you come up with. To be honest, the way you keep adding more and more calculations on top of the framework I came up with for your initial question, it might be time to start considering a refactor. You've got a number of functions that are all pretty much the same except for the particular calculation at the core of the function. So you're doing a lot of the same work over and over and over. For instance, you are fetching the results of the chunked Of course, there is something to be said for the approach of getting it all to work as you want it to work and then refactoring. But that can sometimes be a big hassle. |
|
@roosterboy Thank you for taking time to look at my mess. I appreciate the critiques and will give it another whirl. |
|
@roosterboy I would love to refactor at this point... but I haven't the foggiest idea on where to start or how. I feel like a toddler learning how to take my first few steps walking from the coffee table to the couch by learning how to make these basic functions actually work. Refactoring the entire code seems like the equivalent of a track coach improving the running form of his high school student athlete. Probably not an accurate analogy, but that's how it feels from this very humbling position I'm at. I'm afraid my level of conceptual understanding is not high enough yet. Each day is a battle to subvert the increasing bubbling frustration and resist the urge to give up entirely. Here is an easier to read version of calc401k
I think my
However, in I'm rewriting my I'll keep working on it. |
|
Eh, y'know, I probably shouldn't have even mentioned it. It's far more important to understand the code and make sure it works than to clean it up. There will always be a time for that later. Don't sweat it.
That's what's known as trailing closure syntax. Essentially, if a closure is the last argument to a function, you can drop the argument label and put the closure's brackets outside the parentheses. And if there are no more arguments, you can even drop the parentheses altogether. So this:
is the same as this:
It can get a little more complicated than that, since a recent Swift change allows you to use trailing closure syntax with multiple parameters rather than just the final one, but that's the gist of it. Either way you want to write your function calls is perfectly fine though some might say using trailing syntax is a bit more "Swifty". You'll definitely see it more as you start to read other people's code. I recall that after Swift first came out and before I really sat down to learn it, I would look at code using trailing closures and think "WTF is going on here?!?"; it was quite confusing.
Post your latest efforts and I can take another look tonight. |
|
@roosterboy Thank you for sticking with me! I've been experimenting with loops and I'd like to know if you think the following approach would be permissible... but first, I have a question about protection against out of range parameters. Here is an uber simplified example to highlight the question: I've got here an array of three elements, each is the average of a year's deposits into a 401k. Only three years: the first year was an average of $100/mo deposit, second year: averaged $200/mo, and last: $300/mo.
Assuming the employee's existing 401k balance is only $1000:
Here is my updated formula to calculate yearly 401k balances:
When calculating year two's balance the principal will be year one's balance. When calculating year three's balance the principal will be year two's balance. When calculating year one's balance the principal will be the existing balance variable ($1000). The following is the code that I think will work if I can direct the principal parameter to look to the existing balance variable when the index is out of range.
Question: how do I default the More broadly speaking, I think this approach may work in my project but in order to use a for-in loop like above I think I will need to create a That's the direction I think I want to go, but do you see any fatal flaws in that plan? |
|
You could do something like this:
I must confess to some confusion over how to answer this. It's awfully hard to do without seeing everything in context, especially how your various functions are being called. I would suggest two things:
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
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.