|
Hi guys,I'm in the challenge too and I would love to learn how you would improve my code does the code look good for you? in my opinion is too long, but it works, is there another solution?
|
|
hi, you might want to take a look at Paul's How to convert units using Unit and Measurement article. most of the conversions you are doing are already built in to Swift, as long as you keep track of the UnitLengths of the input and output. alternatively, you could collect all those constants into arrays (i think 5 x 5 will do it) so you have one conversion statement based on the indices hope that helps, DMG |
|
@delawaremathguy can you please teach me how to do it using the Built in swift conversion ? |
|
I have to agree that doing this with the built-in measurements and using it's conversion makes for much more elegant code. Especially if you have already covered Dictionaries. Your dictionary would have Strings for keys (the same words as in your array) and the mesurement (UnitLength) for values. You can then just use a function that would do the conversion, by using the array elements to determine the UnitLength. If you read the article linked above, and feel you still need more details from me, let me know, and I will hop in and share some bits. In closing, I just want to say congratulations on making it work. It's a great place to start. |
|
hi, i had originally put this together for @sugertag, although i'm happy that @MarcusKay also jumped in on this as well, since there's always more than one way to do these things. my suggestion, using built-in conversion units, can be simplified in the case of converting from meters to feet (the initial situation specified by
the code is a little wordy, since i emphasized the types above so that you can see how Swift supports Units and Measurement, and see the conversion steps of String to Double, Double to Measurement, Measurement to Measurement, and lastly (by using to generalize, without introducing a whole lot of
now use hope that helps, DMG |
|
@delawaremathguy is right. An array would be simpler than a Dictionary. His comment is great at showing what is happening step by step before the simplification. So I would recommend going through it first. And just in case you want the simplified version:
The key thing to remember is that your |
|
Thank you for all the above comments. @delawaremathguy you really simplified it so it was much easier to understand the process. @MarcusKay thank you for the simplified version also. With both i eventually understood the whole process. |
|
@alvarovs89, I did mine a little differently writing functions to convert to a base unit (Kelvin in my solution) and then using a dictionary to lookup the converters from the source units to Kelvin and from Kelvin to the destination units. I guess it's a similar idea to Units and Measurements but not as general purpose.
|
|
@david149 The benefits of using Units and Measurement are that on the one hand, you don't need to have multiple functions and therefore it simplifies the whole process. On the other hand, it minimizes the risk of errors. If your formula contains a single digit wrong Xcode won't flag it and you end up with faulty conversions. That being said, you can still simplify your code and get some "general purpose" by removing the 2 dictionaries and changing your functions to the following:
And then all you have to do is change your
It is not necessarily the most elegant solution, but it does the job as you intended it. Also, in order to provide a bit more clarity to your functions, then our conditions could be set to the array instead:
This way, when we read the guard statement we know precisely what the unit is, instead of having to scroll up and remind ourselves of it. You can do the same thing for the The beauty of programming is the many ways you can solve the same puzzle. Which is a great way to practice actually. Once you get your code working properly, try to see how you can improve it. That, in my experience, has been the best way for me to learn. |
|
|
|
Since we are all learning here, I went back to the challenge and thought I'd share an alternative we haven't approached yet. Creating an array where each element is a Tuple.
This has a couple of benefits as I hope is apparent below, but first, some information. In order to access the values of the tuple seperately, we use the following: So when we create the picker, we are still using the
What I like about this approach is that, by coupling the name (String) and the unit type (UnitLength) in a tuple, it allows us to add as many units as we want later on, without having to edit 2 different properties or arrays. Just add a new tuple, of the same type. In the above example edit: just to be clear, "the result remains unaffected", refers to the fact that we don't need to change anything in our code if we shuffle the array around. The displayed units will change their order yes, but that has no impact on conversion. Hopefully this provides some insight and can be helpful to anyone reading this, maybe for some project you might be working on. |
|
Sorry to revive this thread, but as I wanted to experiment with making it a bit more advanced, I stumbled onto something that makes the whole thing even "cleaner",
Notice how we remove the tuple access for converting, and in the I feel this is much better. Hope that helps!! |
GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.
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.