|
I have a question regarding gereralizing throwing functions, specifically with regard to addind the do/catch to it's own function. The examples in 100 Days of SwiftUI as well as other documentation I found online shows that to use throwing functions, we should create an enum, a function, and then use do/catch to try the code. My question is where should the do catch go? What is the best way to generalize it? In the Day 8 example that I used to guide my code for Checkpoint 4 - Integer square, it seems that I should hardcode the square. See the example in the the section labeled "START ORIGINAL EXAMPLE" where I created a function, "integerSquare" I then decided to to rename "integerSquare" to integerSquareRunner, that gets called by "integerSquare." That way, "integerSquare" could be called with different inputs, such as: integerSquare(of: 15) That code can be found in the section labeled "START UPDATED EXAMPLE" It works as I intended, but is this good practice? Is there another way that this same functionality can be accomplished? Thank you in advance...
// **** START ORIGINAL EXAMPLE /*
*/ // **** START UPDATED EXAMPLE
//**** END UPDATED EXAMPLE |
|
|
|
You should put the Creating a whole new function just to have somewhere to catch errors is overcomplicating things. Your first example is the way to go. |
|
You can reduce the amount of zeros here to 100.....
You are returning i x i from range 1 to 10000. 100 x 100 is 10000. 100 x 101 is higher than 10000, so there's not much point in that. As far as do and catch goes try not to write code that will crash :)! |
|
I would change a few thing (not sure that you have covered this in the 100Days) but use a
Then in the
As you can see that handling errors at the call site to display to user. |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.