Today you’re going to face your first challenge day, which is a day where you’re asked to build a completely new app from scratch using what you’ve learned so far. This might come as a surprise to you, because you may well think you haven’t learned that much so far. Well, let me explain…
The 100 Days of SwiftUI is the second 100 Days curriculum I’ve written, and while I know the original 100 Days of Swift was extremely popular and helped a lot of people, I definitely felt afterwards “if I were to do it all again, here’s what I’d change…”
One of those things was going back and adding more virtual chats to the initial Swift days, to help folks really make the most of those fundamentals. But the second change is one you’re meeting today: I want to get you writing your own projects faster.
In the original 100 Days folks completed the first project, then the second, then the third, and only then were asked to write their own app from scratch. While that worked well enough, I realized in retrospect that going on to a second and third project without really having solidified the basics wasn’t ideal.
And that brings us to today: your first challenge day, where you’re going to build a complete app from scratch. Don’t worry: this project has specifically been chosen based on what you’ve learned so far, so everything you need to know was covered already in project 1.
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Challenge 1. As well as solving the challenge, we also walk through two awesome extensions making it do even more! If you don’t already subscribe, you can start a free trial today.
You need to build an app that handles unit conversions: users will select an input unit and an output unit, then enter a value, and see the output of the conversion.
Which units you choose are down to you, but you could choose one of these:
If you were going for length conversion you might have:
So, if you chose meters for source unit and feet for output unit, then entered 10, you’d see 32.81 as the output.
You know almost everything you need to build this. In fact, if you go for a simple solution – and I hope you do – the only actually new thing you’ll find is that you need to use a different format
option for your text field, because .currency
and .percent
don’t make sense here. You should choose .number
instead, which is suitable for standard numerical input.
You know everything you need to complete this project, but if you’re hitting problems then I have some tips that might help.
First, and most importantly: keep it simple! You’ll have lots of time in your future career to think up clever coding architectures and more, but in this initial challenge I want you to go for the simplest solution you can find.
Second, all our unit conversions are simple mathematics, but you shouldn’t try to write conversions to go from every source unit to very other unit. A better idea is to convert the user’s input to a single base unit, then convert from there to the target unit.
So, rather than writing code to convert from liters to milliliters, and from liters to cups, and from liters to pints, and so on, a better idea is to convert the user’s input into milliliters (the lowest common denominator), then convert from there to whatever output unit they want.
Third, as we have three user values here – their input number, their input unit, and their output unit – you need to have three @State
properties to store them all. You’ll need a TextField
, two pickers, and a text view to show your output, and that’s about it. You can break your form up into sections if you want, but it’s not required.
Fourth, the easiest way to store your conversion units is to have a simple string array, which you can loop over using something like ForEach(units, id: \.self)
.
Finally, if you’d like to be a bit fancy with the output number you display, try calling .formatted()
on it – e.g. someDouble.formatted()
. This will cause iOS to format the number for printing, adding thousands separator and removing a lot of the noise from unnecessary decimal places.
Go ahead and get started now. Don’t worry if you need to refer back to the WeSplit project – that’s perfectly normal, and is all part of the learning process.
Good luck! You can do this. And once you’re done, tell other people: you’ve built another SwiftUI app, and this one was entirely designed by you.
You should be proud of what you’ve accomplished.
Tip: Even though you can absolutely complete this project using simple arithmetic, you might be interested to know that Apple does give us dedicated functionality for doing unit conversion – see my article How to convert units using Unit and Measurement if you’re curious. However, I should reiterate that it is not required to complete this project: you can convert from liters to pints (for example) just by multiplying the input by 2.11338.
Great job on finishing another day! If you still have questions about what you learned, please send them in using this form – I'll do my best to address the most common problems in the next course update. Thank you!
If you use Twitter, the button below will prepare a tweet saying you completed today, along with a celebratory graphic, the URL to this page, and the challenge hashtag. Don't worry – it won't be sent until you confirm on Twitter!
Alternatively, copy and paste the text below to your preferred social network - I'm @twostraws on Mastodon.social, Bluesky, and Threads.
🎉 I just finished Day 19 of the #100DaysOfSwiftUI at https://www.hackingwithswift.com/swiftui/19 via @twostraws
Need help? Tweet me @twostraws!
SAVE 50% All our books and bundles are half price for Black Friday, 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.
The 100 Days of SwiftUI is a free collection of videos, tutorials, tests, and more to help you learn SwiftUI faster. Click here to learn more, or watch the video below.
Link copied to your pasteboard.