Tip Calculator
Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 48 of 50.
Challenge
MediumBuild a tip calculator in a VStack:
- a
TextFieldwith the placeholderBilland the identifierbill, bound to aStringstatebillText - a segmented
PickertitledTip(identifiertip) bound to anIntstatetipPercentthat starts at15, with the options10%,15%and20%tagged10,15and20 - a text
Tip: $7.50(identifiertipAmount) and a textTotal: $57.50(identifiertotal), 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