Menu
Coddy logo textTech

Tip Calculator

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

challenge icon

Challenge

Medium

Build a tip calculator in a VStack:

  • a TextField with the placeholder Bill and the identifier bill, bound to a String state billText
  • a segmented Picker titled Tip (identifier tip) bound to an Int state tipPercent that starts at 15, with the options 10%, 15% and 20% tagged 10, 15 and 20
  • a text Tip: $7.50 (identifier tipAmount) and a text Total: $57.50 (identifier total), both with two decimals

Read the bill with Double(billText) ?? 0, so an empty or invalid field counts as 0.

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