UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 1

Paul Hudson    @twostraws   

Updated for Xcode 15

You already know enough to start writing your first useful code, albeit fairly simple: we’re going to convert temperatures from Celsius to Fahrenheit.

Your goal is to write a Swift playground that:

  1. Creates a constant holding any temperature in Celsius.
  2. Converts it to Fahrenheit by multiplying by 9, dividing by 5, then adding 32.
  3. Prints the result for the user, showing both the Celsius and Fahrenheit values.

You already know everything you need to solve that problem, but if you’d like some hints then I’ll add some below.

Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Checkpoint 1. If you don’t already subscribe, you can start a free trial today.

Note: I really do encourage you to try building this playground before reading any hints or trying my solution. I know it might seem simple, but the course does start to get harder soon and it’s important to be sure you’ve learned all the fundamentals.

Please go ahead and try building the playground now.

Still here? Okay, here are some hints:

  1. Use let to make your constant. You can call it whatever you want, but I think celsius would be an appropriate name.
  2. Celsius is commonly stored as a decimal, so make sure and create it as one. This might mean adding “.0” to the end – using 25.0 rather than 25, for example.
  3. We use * for multiplication and / for division.
  4. Use \(someVariable) to activate string interpolation.
  5. If you want to get fancy with print(), you can use Option+Shift+8 to get the degrees symbol: °. This means you can write something like 25°F.
Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.