Swift for Good: Volume One by Paul Hudson

Swift for Good: Volume One by Paul Hudson

Author:Paul Hudson
Language: eng
Format: epub


When we combine those things, some important behavior emerges: it’s almost free to pull some code out of a SwiftUI view and place it into a new view of its own. Yes, there is a small cost because SwiftUI still needs to collapse our view hierarchy before it can be rendered, but that cost is minuscule – so low you won’t even notice it even if you aggressively break out your views. Apple even provides us with a simple recommendation to drive the point home: “you should never hesitate to refactor your SwiftUI code because extracting a subview has virtually no runtime overhead.”

In our ContentView code we have all the work to lay out a single row mixed in with the code to present the list and a navigation view. The job of ContentView really ought to be limited to showing a list of users, not to decide how each row should present itself – by putting all that code in one place we now can’t use it elsewhere, we can’t easily switch layouts in ContentView, and we’re also making the code harder to read.

So, your first logical step is to extract that part of the view hierarchy into a new view like this one:

struct SummaryView: View { let item: User var body: some View { NavigationLink(destination: DetailView(item: item)) { Image(item.id) .resizable() .frame(width: 44, height: 44) .clipShape(RoundedRectangle(cornerRadius: 10)) .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.black, lineWidth: 1)) VStack(alignment: .leading) { Text(item.name) .font(.system(.headline, design: .rounded)) Text(item.job) .font(.system(.body, design: .rounded)) .foregroundColor(.secondary) } } } }

And now ContentView becomes trivial:

NavigationView { List(users, rowContent: SummaryView.init) }

This is a great step forward: ContentView now focuses on navigation, so you can add a title and bar button items, while SummaryView focuses on what it means to display one piece of data.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56796)
What's Done in Darkness by Kayla Perrin(26814)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20680)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20455)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20451)
The Fifty Shades Trilogy & Grey by E L James(19323)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19272)
Shot Through the Heart by Mercy Celeste(19136)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17302)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17237)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17084)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(17006)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16623)
The Subtle Art of Not Giving a F*ck by Mark Manson(14628)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14318)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13985)
Scorched Earth by Nick Kyme(12970)
Drei Generationen auf dem Jakobsweg by Stein Pia(11155)
Suna by Ziefle Pia(11083)
Scythe by Neal Shusterman(10562)