|
Had some difficulty doing Checkpoint 4, ended up with a solution but didn't quite understand everything that happened, specially with the global return error, so wanted to clarify that. Here's my code:
My question is related to the function creation. First i had an else statement after the second if with the There's a user with a similar problem: https://www.hackingwithswift.com/forums/100-days-of-swiftui/checkpoint-4/15586 But here i wanted to understand better the difference of having a return inside an if statement and outside. If i have my result returned inside an if statement, swift will give me an error, is the best procedure to repeat the same return oustide the if statement? |
|
Renato wonders why his code doesn't cover all the cases...
To see the compiler's point of view, I simplified your code to essential elements.
From this snip, convince yourself that the loop inside the function may never run. Consequently, the function never has the opportunity to return an Because your code can throw errors, it makes sense to add throw |
|
Thanks for the help. It makes sense after your post. I've tested on a playground with the value 0 and i understand the error it gives because with that value, the range would be from 1 to 0, but in that case, since the loop doesn't run, instead of giving me a system error like this: Swift/x86_64-apple-macos.swiftinterface:5782: Fatal error: Range requires lowerBound <= upperBound Shouldn't it give the error string i used for that specific enum value in my code? |
|
Renato found a bug!
Nice code review. You found the bug, you found the root cause. But you missed on the way to report it. Hint: What error condition might you add to your Next, figure out how to catch that error when it occurs and how to Keep Coding!Report back and show your revised code! We'd like to see what you learned. |
|
I came up with this:
I tried to catch the "value = 0" error doing an if statement before the for loop and it worked. Is this what you were expecting or there's a better solution for this? I'm asking this because i covered this problem with the value 0 but i feel like that if another error occurred, my default When does the default catch error gets in action? |
|
Since |
|
Renato starts to bug-proof his code! Excellent progress.
Hope you don't think we're ganging up on you! Glad to see you revisit your code with updates! As you gain experience, and kill more bugs(!) you'll start to think of the edge cases and build these checks in from the start. Indeed you fixed the one case where your input number is zero. But you could still accidentally try to pass in a negative number causing the same crash. So consider this instead:
Then if you try zero or lower, your code will throw the error! In future lessons, you'll love deploying your guards to protect your functions. See -> Castle Guards Keep Coding! |
|
Thank you all for the input. Reading Now the function will first see if my number is within bounds and if it is, it will move on to the next part, instead of continuously looping between the range of 1 and the choosen number to see if it's always between bounds.
|
|
Renato posts an update:
I tried your code with:
Your code throws a |
|
Yeah, should have tested with more numbers instead of only 0. So the way i had my code, it would throw the noRoot error instantaneously in the first loop of the Here's my code:
|
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, 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.
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.