KEMBAR78
SwiftUI Differences Cheatsheet | PDF
0% found this document useful (0 votes)
29 views3 pages

SwiftUI Differences Cheatsheet

This document outlines key differences in SwiftUI, focusing on property wrappers like @State, @Binding, @ObservedObject, and @StateObject, explaining their ownership and lifecycle management. It also compares List and ForEach for displaying collections, and discusses onAppear versus onReceive for handling view lifecycle events. Additionally, it contrasts NavigationView and NavigationStack, highlighting the advancements in navigation management in iOS 16 and beyond.

Uploaded by

kg.ios2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

SwiftUI Differences Cheatsheet

This document outlines key differences in SwiftUI, focusing on property wrappers like @State, @Binding, @ObservedObject, and @StateObject, explaining their ownership and lifecycle management. It also compares List and ForEach for displaying collections, and discusses onAppear versus onReceive for handling view lifecycle events. Additionally, it contrasts NavigationView and NavigationStack, highlighting the advancements in navigation management in iOS 16 and beyond.

Uploaded by

kg.ios2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SwiftUI Key Differences Cheatsheet

@State vs @Binding

@State:

- Declares a local source of truth within a view.

- The view owns and controls the state.

Example:

@State private var isOn = false

@Binding:

- Binds a state from the parent view, allowing read-write access in the child.

Example:

@Binding var isOn: Bool

Usage:

Parent declares @State, passes it with $isOn to a child using @Binding.

@ObservedObject vs @StateObject

@ObservedObject:

- Used when the object is owned outside the view.

- Will recreate on every view reload.

@StateObject:

- Used when the view owns and manages the lifecycle of the object.

- Keeps the object alive across view reloads.

Example:

@ObservedObject var viewModel: MyViewModel

@StateObject private var viewModel = MyViewModel()


SwiftUI Key Differences Cheatsheet

List vs ForEach

List:

- Creates a scrollable list of views.

- Includes built-in styling.

ForEach:

- Iterates over a collection.

- Used inside stacks (VStack, HStack) or inside List.

Example:

List {

ForEach(items, id: \ .self) { Text($0) }

onAppear vs onReceive

onAppear:

- Called once when the view appears.

- Commonly used for setup or API calls.

onReceive:

- Listens for a Combine publisher and responds to each update.

Example:

.onAppear { fetchData() }

.onReceive(timer) { time in update(time) }

NavigationView vs NavigationStack
SwiftUI Key Differences Cheatsheet

NavigationView (iOS 13+):

- Older navigation container.

- Still used in apps targeting iOS 13-15.

NavigationStack (iOS 16+):

- Type-safe, supports programmatic navigation via path.

Example:

NavigationStack {

NavigationLink("Next", value: "Detail")

.navigationDestination(for: String.self) { value in

Text(value)

You might also like