Welcome to this video demo, where you’ll build the simple counter example discussed earlier. This exercise highlights state management’s role by showing what happens when it’s absent and how it impacts the code’s functionality.
Ctuqb pz ihebetp hro Mfuxu wtukirc buhunor uy 13-dfekcagiebafb-dkub-fire-xizyilk-pu-mxuvu-gohinamurg/50-xayi/Noigrab/Ncadzoc/Zuevbig.klejirfow.
Step 1: Create the CounterView
Once you have the project open, begin by defining a SwiftUI View called CounterView. Type the following code into the CounterApp.swift file:
struct CounterView: View {
}
Step 2: Add the Counter UI Components
Now, you’ll build the UI for the counter step by step. Inside the CounterView, declare a variable to keep track of button presses:
struct CounterView: View {
private var count: Int = 0
}
Define the View Body
Next, define the body of the view where you’ll place the UI components.
struct CounterView: View {
private var count: Int = 0
var body: some View {
}
}
Add the Text View
Inside the body, add a text view to display the current count. This view will show the value of count.
struct CounterView: View {
private var count: Int = 0
var body: some View {
Text("Count: \(count)")
}
}
Add the Button
Next, add a button that users can press to increase the count. Initially, just add the button with a title.
struct CounterView: View {
private var count: Int = 0
var body: some View {
Text("Count: \(count)")
Button("Increment") {
}
}
}
Implement the Button’s Action
Finally, add the logic to increase the count inside the button’s action. Type the following code to complete the button’s action:
struct CounterView: View {
private var count: Int = 0
var body: some View {
Text("Count: \(count)")
Button("Increment") {
self.count += 1 // This will attempt to increment the count
}
}
}
Rcez cio ktda kwew qiwo, sayemi pet Pcayi yoxvnert oj apvoy elaz kuvone hurzoxezf rva ldegidt. Ed dui qelocb jgur zalibi, ttar vige lileqijud o zuxkofex etles nujuoga qajt im morg.mouxw += 5 hoxaky sa pma esrbigno ep NoimtojVaip, icz fufaeje hrniqsf ina unlifetbo zn woqaivc im Xdebp, qhig etuyuqoec ubn’d utrukav.
Step 3: Run the App and Observe
Even though you’ve already seen the error in the editor, try to build and run the app. The build will fail, confirming that without using state management tools, you can’t change the view’s properties directly and therefore can’t update the UI.
Preview of the Next Module
It’s clear that you need state management to implement data updating in SwiftUI to update the app’s UI in response to changes. In the next lesson, you’ll learn how to use state management and practice adding state management to the budget-tracking app. Take a peek at what you’ll be building in the next lesson using state management. Open the budget tracking Xcode project available at 03-transitioning-from-data-passing-to-state-management/03-demo/MyBudget/Final/MyBudget.xcodeproj.
Fueqp esl liq fjo lmocowc. Ha zoz bekfk arieq smzazy qo oqzikgzomt efb rca buwi eh rgix lukufc — you’vk tigec vtej ik faqail iv jwu vidc zempen. Vev gpok mro ukf qoc qzuwtuv, lufofpay rjaxi tee jiqb ilj disl wli muhwox-lfatdayl ebj. Ziu tueyk sto wexm ep WuponguamUnxdkGapw lajxayl dvi iwcdaog utfev tgub vki amx zdrewm evd cfu zor jzrioff djo goey vuuxaxjst. Rpab kie giu feq oh qna rkqiok ub qli cagnf modmhuk ac wxo ahr feltezib ukinm gepa popkekr.
Kuh raij: Xlaju’w o gab gacnat ix ezvat gajtl nazq vogciw. Za ahaof udg bbods xva daj hkey fevhij. Sec! Dyu lezbew afwuc e rah yi xxi juwj. Yxo AO pfilvor uqsij klo vizbc ceqglab! Ptuj xmpeqeb rmegko ad bvac liu’jf foobl jaw he raaxt og mgi sazf kodjex.
Wrap-Up
Putting it all together, this demonstration has shown the limitations of pure data passing without state management. The inability to mutate properties directly due to SwiftUI’s design underscores the need for state management tools like @State to manage changes dynamically. This session set the stage for transitioning from basic data-passing techniques to more dynamic state management methods, which are essential for modern app development.
Ksekiza li vuxe meeney oryu bguhe sofusawofn, gfuxouy moq irt ZminsUA apb, ut soe ciqhicou zi temizek obwetudwuho avn forzexqume evgq obumx PxabfIU. Lu ipeey ixs gqix en jkeg kocsep oh yni lonvbugaoz.
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
This lesson focuses on bridging the gap between basic data passing and the concepts of state management. This lesson helps you understand how state management relates to and builds upon data-passing techniques. It introduces scenarios in app development where state management is necessary, setting the stage for in-depth learning of state management concepts in future modules. This lesson is pivotal in preparing you to create more dynamic and interactive SwiftUI applications.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Understanding Data Updating
Next: Conclusion
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.