Understanding SwiftUI View

Why is View a struct and not a class anymore?

Why??

Body of a SwiftUI View

public protocol View {
associatedtype Body : View
var body: Self.Body { get }
}
struct ContentView: View {
var body: some View {
VStack {
Text("Hello Swifty !!")
Text("I am amazing !!")
}
}
}

What is the some keyword here?

What if you do not want to use some View?

self is immutable in View body

struct ContentView: View {
var title: String
var body: some View {
VStack {
Button(action: {
self.title = "I am changing this"
}) {
Text("Hit me")
}
}
}
}

How to fix this?

@State var title: String

What happens if we make SwiftUI view as a class?

class ContentView: View {
var body: some View {
VStack {
Text("")
}
}
}

What is the above error?

protocol Producable {
var producedFactory: Factory<Self>? { get }
}
class Factory<T: Producable> {}class PharmacyProduct: Producable {
weak var producedFactory: Factory<PharmacyProduct>? {
nil
}
}
class Paracetamol: PharmacyProduct {}
final class ContentView: View {
var body: some View {
VStack {
Text("")
}
}
}
Crash !!

I would love to hear from you

--

--

--

Software Developer(iOS), Speaker & Writer at Swift India

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why would you use a protocol associated type?

Mastering the fundamentals of BLE for iOS using Swift.

Adjusting Constraints for layout items in a UITableViewCell

Testing Apple’s integrated Swift Package Manager

Environment Values as an Alternative to Dependency Injection in SwiftUI

Theme management with datastore in Jetpack Compose

Create your first AR app with RealityKit and SwiftUI

Swift 5.7 | What's new in UIPageControl ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
G. Abhisek

G. Abhisek

Software Developer(iOS), Speaker & Writer at Swift India

More from Medium

SwiftUI. Add specific action in Widget

Why SwiftUI will make UIKit obsolete

Guide How To Display Modal Alert on iOS With Help of UIAlertController

Alert vs ActionSheet by Apple

SwiftUI LazyVGrid and LazyHGrid