|
Can it be possible to put a switch case inside a function that recieves a String and returns a Double? Would that even work? |
|
Rodney posts a coding challenge!
Challenge accepted!
Do I win a prize ? |
|
You can as
This will print 1.0 as correct but however strings are case sensitive so if you had
or had any "spaces" in
then would print the default value of 0.0 So then you would have to add some extra code to fix this
This will print 1.0 for all |
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! |
|
|
|
Rodney doubles down:
Of course! But both Nigel and I have given you enough tutorial material for you to figure this out on your own. Perhaps you might tell us what your business case is. Also try it on your own and post your code with comments about your approach. If you fail, this is great! Point out what line fails and explain what you attempted to do! Failing is learning. |
|
Already done and thanks for the info... I went back to Pauls app (UnWrap) for reference examples and I got my apps function to work as I expected. Now to make it look nicer... |
|
My switch case exceeds the limit of the web site so I can't post the whole code. The code accepts a breed and sex for the dog and then takes those two Strings combines them and returns a double for the ideal weight for the entered dog and its sex... Theres probally a more efficent way of doing this but oddly this works as expected.
|
|
Nice. Thanks for posting code. It's time to consider views. As much as you can, think of views as ways to project onto a screen the contents of a struct that just holds data. Review your code and think about removing struct code that executes business rules. For example, finding the ideal weight of a breed, based on gender. The view should not concern itself with this! The view should focus on displaying the ideal weight, the gender, and the dog's breed. Doing the calculations is someone else's business! Here's a different way to approach your business problem. Paste into Playgrounds, and play around with the business rules.
Thanks to @rooster for capitalization reminders. |
|
So is this portion of the code whats called a dictionary?
|
|
This is orthogonal to the question at hand, but:
In Swift, you should really name your types starting with a capital letter. That's the convention and that's what will make sense to other people reading your code. So:
That's actually an array of |
|
ArrayNo, this portion of code is a collection of dogs. This collection is actually an If you want to find a particular breed, you have to search each See-> Array functions You provide a test, and the DictionaryThink of a common dictionary. You want to know the definition of cromulent? Well, you use that word as a
Notice these are not
Do you want to add a new term to your
|
|
Someone once told me if you don't ask stupid questions you just stay stupid... I think I've exceeded my limit thanks for your help and examples. |
|
Rodney makes an observation about limits within the HWS forums:
Rodney: You're welcome. Good Luck!I hope I didn't give you any impressions that I thought your questions were stupid. I enjoy answering questions and thinking of detailed answers. I enjoy thinking of different ways to explore and explain Swift, design, or architecture concepts that may give new developers heartache. However I've received feedback that my answers are perceived to be condescending, perhaps snarky. I want to remove heartache, not cause it. I wish you luck on your journey. I will probably be taking a break. |
|
No offence taken I was just attempting a little humor didn't mean to offend... I had actually tried a Function with two strings before your I saw your answer but am always amazed at many ways a problem can be solved. I appreciate the help on this forum as it expands my knowledge in the process. |
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!
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.