Daily iOS
SwiftUI

Single state tally screen

A counter screen built in SwiftUI has increment, decrement, and reset buttons plus a display of the current value. How would you arrange the state so the displayed number can never disagree with the actual value after a button press?

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

Keep only a single integer for the counter value in @State or an @Observable model, and compute the displayed number and whether the decrement button is enabled from that value. If you don't store a display string or flags separately, there's only one stored value, so the display and the actual value have no way to disagree.

Share a question