Daily iOS
SwiftUI

State-driven view switch

This is a 15-minute SwiftUI live coding exercise. Tapping an "Add" button should show a quantity stepper (minus/plus) in the same spot, and when the quantity hits 0 the stepper should disappear and the "Add" button should come back. How do you set up the state and structure the views?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

One piece of state, the count, is enough: draw the Add button when count == 0 and the stepper otherwise. With no separate Bool flag, put the two views in a ZStack or a fixed frame so they occupy the same spot.

Share a question