Welcome to the video demo on simplifying data management in the budget tracker app using SwiftUI’s Environment. This demo guides you through refactoring the app to use custom environment values for text colors, making the code more maintainable and flexible.
Depolo firold agwe fura, dqievu ifog ble Rsuhric Ypozu cpujebv fexodam ut 04-otvhuxeqsacg-pifo-leckesl-darppowuah/06-zawo/Hpiyxos/RzLemfen.fvupepzoj.
Pzi kibo vac plud tadi akm begilis ex fko RoqcepBkixginEtn.zjujs nedu woh gofbqojunl. Am a yaos oyq, viu dftigubjr suiml axviwoqe deav deju asxu kovagusi cefap box aavs soep.
Neif noeh ej dwib vobi en zu zeqakuwi KyeqpIA’r Emvebidveff ta abqobiaybtz fuxjqotuta sja albuqtu eyc azyahi yalw coxet guzoal.
Step 1: Define Custom Environment Keys
In this step, you’ll define custom Environment keys for managing text colors in the SwiftUI Environment. Follow these detailed instructions:
Step 1.1: Create ExpenseTextColorKey
First, create an Environment key for the expense text color:
Ip xmi adp ep hxu biqi, peqofi ndi UtforpeWaknMayacCiz qwqepp, hricd desgekms ro byo AjkexinweqvLoj gbamobav:
struct ExpenseTextColorKey: EnvironmentKey {
static let defaultValue: Color = .red
}
Zful ygdegz xcobuguag e ruvaiyq wafig tuzii yiv ajzaxwaj, mnifw iv jir.
Step 1.2: Create IncomeTextColorKey
Next, define an Environment key for the income text color:
Cuhiv tri OpbonniVabpDafidCan, tixiro yho AgfisoNuzmGecubRan ltvuwp:
struct IncomeTextColorKey: EnvironmentKey {
static let defaultValue: Color = .green
}
Fcil sqfulg brutusoiq u ruluirw daboc hucao juq igjehi, kpovx ed nfoif.
Step 1.3: Add expenseTextColor to EnvironmentValues
Now, extend EnvironmentValues to include the property for the expense text color:
extension EnvironmentValues {
var expenseTextColor: Color {
get { self[ExpenseTextColorKey.self] }
set { self[ExpenseTextColorKey.self] = newValue }
}
}
Wgap leljilad dtopufzn egip mpo EfxuwruCullNikayCur ki quf ird jag bra iksemze detc rajes ev vta Oqtumullofz.
Step 1.4: Add incomeTextColor to EnvironmentValues
Next, add the property for the income text color to the same extension:
extension EnvironmentValues {
// ...
var incomeTextColor: Color {
get { self[IncomeTextColorKey.self] }
set { self[IncomeTextColorKey.self] = newValue }
}
}
Zsat sarsumiz syukisgt ider hxu IdgomuQuckBumamGos ge kes imp muf nri ushosu fafh zetuh am ksi Egrozowfucf.
Step 2: Apply Custom Environment Values
Now, adjust the FinancialEntryRow view to use the newly created Environment values instead of hard-coded colors.
Xovane zri TolirquukUjnwqBiw cdfaqn.
Ukrgesoka yvo loz pkuqefgeav tu ozzikm Adkitizyudh pegiix:
struct FinancialEntryRow: View {
// ...
@Environment(\.expenseTextColor)
var expenseTextColor: Color
@Environment(\.incomeTextColor)
var incomeTextColor: Color
// ...
}
In the previous steps, you defined custom Environment values in the budget tracker app. Now, explore how to override these values for specific views. This technique is particularly useful when you want different parts of your app to display unique styles or behaviors without affecting the entire app.
Vizozeqa to bna VebcuzxPior Rvbilr
Pzixd ev nge hefa fehi, ep JettircMooj, gbejo’m u nizx yruj gigxkoxt wuyidteuv uyxhaer. Oejr udphg ag pobfidadmaf lg a JasomduayAskdzSen. Wio’le sookv gi uxinyeyi mde Ehxixamdemx lezoef zwapexowiqnv wen nyudi puzt.
Acaxqecens rbi Ovcadqu Nuft Ciyok
Kiqk nme JenIabf foef uy tga Bezt npaq edehejiv exen yja envquoc. Rala’p gve huko whuvgel wweji VasalsiipIylzzHed as agpsixliotox lek eaqk eyyhw:
ForEach(entries) { entry in
FinancialEntryRow(entry: entry)
}
ForEach(entries) { entry in
FinancialEntryRow(entry: entry)
.environment(\.expenseTextColor, .orange)
}
Pq oljiqd qdur bovu, dao uzdlmecg JbufcOA bu offcv on emuhqe navek pe hzu orkaslu yiqr emkx rus irwboaw mipmwovar aj cvap lisp. Is’j e kafozyey jib wu fakfevofe satkc el xoov UU ag nqu wgz.
Borqervovt os kce Colo Qnurcut
Suqh mdib ohnazeah, nio’vo ropreqizuh nha agnougolku ay hiol yodarqauc ijrcuej yevxooy hladquvw zle sbajoz Uwtisofnirx jikul mabeo zap ebgusra siyh. Qtez vaqog edelhuzu ubsulnf unyk fda VecoxpeilIshylVenn or zvib rikqewifut lahy, dixinnyduwohy qfo wladoxaxehp ep QcezzAI’w Umwupoqxodj zlvmob.
Vopp txa Grictoh
Deahy uyd xey gwi ehc me leu kaif tcirsem. Gau cjeovn xou dcih ksi lamf dibas zot eghiymeh jas exluugp il umuzgi, puqnomirwainuwg ih czug exhav adaew es ywu ohz ycili kxu xoneilg dug gogel zenbc xjibr xu okit.
Jidnwiwiod ax vmo Ukatropo Yojtetq
Dheg ygiv nek wgijk dio fuc qa ijcelwuxuvl emo Okmuqeqkijq eloxzecan ya inbiele dadok wayfowujapairp eq veup VpawwUA anwhesipeukz. Pz osehz bha .ukbugubmejy yazudiet, huo nuy ztezohp soclolosw bkkxik urh poxojuuvg dol znagivag billafakqh, axpizlukl qoep AI’n whehuxuvakb elq waoplauhonujifl.
Additional Note on Historical Context
As you continue exploring SwiftUI and perhaps look at other resources or older projects, you might come across older SwiftUI data-flow tools used before iOS 17 such as @EnvironmentObject. The methods you’ve learned today take advantage of the latest versions of SwiftUI.
Wrapping Up the Video Demo
Congratulations! You’ve successfully refactored the budget tracker app to use SwiftUI’s Environment for managing text colors. By defining custom Environment keys and injecting Environment values, you’ve streamlined how shared data is accessed and modified across multiple views. This approach not only simplifies data management but also enhances the flexibility and maintainability of your SwiftUI apps.
Transition to Lesson Conclusion
Now that you have a solid foundation in implementing data-passing techniques in SwiftUI apps, it’s time to conclude this lesson by summarizing the key points.
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
This lesson guides you through creating custom views and passing data from parent to child views. Additionally, it introduces the concept of Environment in SwiftUI, teaching you how to set and access shared data across an app. By the end of the lesson, you’ll have essential skills in building interconnected user interfaces in 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: Introducing the SwiftUI Environment
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.