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

Local Notifications in iOS Apps

Swift iOS Interview : All about Property and Property Observer

Adding Thousand Separator to Number in Swift 數字增加千分位

Swift, Properties, 4, Lazy Stored Properties, Lazy Variable, lazy

How to make your push notifications more interesting!

Launching a screen within an app using custom Siri voice commands

Views and Controls — The SwiftUI 2 Documentation You’ve Been Waiting for

Deep Inside Views, State and Performance in SwiftUI

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

Swift — Network Requests and JSON Parsing 101

Introduction to Swift UI

Introduction to Combine framework in Swift

apple/swift-evolution 上關於 guard 的有趣討論