Menu
Coddy logo textTech

Quantity And Total

Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 46 of 50.

challenge icon

Challenge

Medium

Keep everything from the previous steps. In OrderView:

  1. Add a @State quantity starting at 1 and a Stepper titled Quantity: 1, ... from 1 to 5, with the identifier quantity.
  2. Add a computed property extra: 0 for Small, 0.5 for Medium and 1.0 for Large.
  3. Show the total, (price + extra) * quantity, as Total: $4.25 (two decimals) in the .headline font, with the identifier total, below the stepper.

Try it yourself

import SwiftUI

@main
struct CoddyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

All lessons in Introduction to SwiftUI

Practice on your own: Swift playground