Recap - Step Counter
Part of the Introduction to SwiftUI section of Coddy's Swift journey. Lesson 42 of 50.
Challenge
HardBuild a step counter with shared data:
- a class
Activitythat is anObservableObjectwith a@Publishedstepsstarting at0and a methodwalk()that adds500 - a view
GoalViewthat reads the activity with@EnvironmentObjectand showsGoal reached!(identifiergoal) oncestepsis1000or more, andKeep going(same identifier) before that ContentViewcreates the activity with@StateObjectand shows a textSteps: 0, ... (identifiersteps), aWalkbutton that callswalk(), andGoalView(), with the activity provided to theVStackthrough.environmentObject
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