|
I solved this task in a very simple way some time ago but I feel this was kinda cheating from my side. So I want to turn the page and move on but It does not let me sleep calm. I checked multiple topics about this challenge how other people managed to solve it and there was a lot of cases(maybe most of) where people used "switch/case" option. It's a great way and it works but in one of the topics @Obelix suggested to use the power of Enums. https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-17-challenge-switch-case-default/15294
So I want to learn how to use Enum to solve this in a smart and efficient way and to be able to use this knowledge further on in other tasks. There's also another case where @roosterboy offers to use Enum to solve the task in "RockPaperScissors Challenge" https://www.hackingwithswift.com/forums/100-days-of-swiftui/rock-scissor-paper-project-crash/10171 I tried to implement the Idea in "converterApp" and also failed. So decided to start from all the beginning. Maybe this way it will work out. Please help me with this. Thank you and yes. Tere are many errors respectively...
|
|
|
|
@ngk seeks help with code.
It's great that you're revisiting your code looking for more Swifty ways to improve. Please paste the following into Playgrounds and run! One lesson you may find interesting is that enums are much more than a list of possible selections. Here you can see code that links an enum case to a Swift defined The See -> Measurement Objects in Swift
Keep Coding!Please return here and let us know what you learned. |
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! |
|
Thank you for the lesson, @Obelix. I already thought you gave up on such a "slow" student as me.
I have no other choice. 1) I literally have problems sleeping. 2) You can't build a reliable wall if you will skip some briks on lower rows. So let's jump into your reply.
As I understand this method thansformed our Enum Cases and printed them as an Array of Strings. At least that's what I can see from the output result ( and using Option + ? pointing on "compactMap" to find out more)
So here, as I understand, I cant specify all cases after "." (And no need in this ?) It shuld be something set by default like in your case conversion from "m" to "dm" without touching the picker. (In my case further it will be "m" to "feet")
Here I gut stuck.
Here too. Sorry sir What type is fromUnit? What type is fromUnitasUnitLength ? - can I assume both of them are Optionals ? As I understand this line: let fromUnit = Conversions(rawValue: "inch") is the way of writing this(or not?) : @State private var convertFromUnit = Conversions.m when cases in our enum are Strings and look like this:
But when I tried to use this option(with "string" cases ) in xcode the wave of red errors fall on me... So for now I'll stick to original example |
|
I immediately tried to implement your examples in field. But as usual I failed : / Here we go:
|
|
You are coding and failing. Then asking questions, then coding and failing. This is the best way to learn. Well done. But I don't want you to get frustrated and give up. Here some code to guide you.
|
|
Good news. Accidentally while experementing I fixed a problem with picker by myself:
So now we can switch between different units and immediately get the output. In such moments I carefully continue to believe that I'm not wasted... |
|
First. My apologies. I remember about your previous lesson and your sensitivity to "ContentView" definition. Although I allowed myself to think this is not the biggest problem for me atm and left it for better times. If I won't be able to write code there will be no difference between: "ContentView", "MomentView" or "SnowLandView" Cause I will still suck in coding. I hope you understand, sir But If you will continue to help me with answers on my questions, I promise, I will get my hands to "Rename ContentView" topic, and further will never use default "ContentView. Cause I honestly hate this default naming. |
|
Second. I think after your Project Code I can understand much more. That's good ! But still not everything. Mooooore questions (I apologize...)
I don't get it. Why using strings ? 1) we know that we can use the .km case from Enum. Then why use strings ? then why use strings in @State variables ? : ) I can understand the need of strings for the picker. Picker needs text values. But as we already know, we can avoid using them in changeFromUnit/changeToUnit. Then why ? Or that all is about getting the same result but in different ways ?
Is this part Enum's computed var you're talking about ? if YES, then I've never been thinking about it as a computed var ...
I still dont get the trick between those two implementations. Why did you use but in your project code you used already I double checked, in Playground both options work. But in Project only that makes my head spin, sir.
Almost forgot What's that and why it is there as the note ? First I thought it's related to upd: @Obelix kindly provided me answers to some of my questions so I'm thankful for his help. Code compiles, app works. Unfortunately I didn't get the answers on all my questions. If I'll find them later - I will update my own topic here. To be able to help other people. Meanwhile topic will be marked as: "almostSolved" |
|
Hello all, This is my attempt. Wins:
Fails:
|
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.