Last active
February 5, 2023 15:33
-
-
Save steipete/ab14fb8f71b2b5c4df1f22f25c1d2999 to your computer and use it in GitHub Desktop.
10 Things I Wish I Knew When Starting SwiftUI (See also https://gist.github.com/steipete/6c430d08bd57b066fada7628d3b8b719)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- In almost all cases where you type `@ObservedObject`, you really want `@StateObject`. | |
If you need to support iOS 13, use `@State` on parent and pass value into child with `@ObservedObject` to simulate `@StateObject`. | |
Pass arguments to that model in `onAppear`. Example: https://github.com/ra1028/SwiftUI-Hooks/blob/main/Sources/Hooks/HookScope.swift#L39-L41 | |
``` | |
.onAppear { | |
model.onAppear(userTag: userTag) | |
} | |
.onDisappear { | |
model.onDisappear() | |
} | |
``` | |
- Don't do work in your `View` struct inits. | |
- Except if your view is dead-simple, you want one model per view (`class XYZModel: ObservableObject`). | |
- `didSet` doesn't work on `@State` but it works on `@Published`. | |
- Give your views a unique `id()` if they don't seem to reload. | |
- [assign(on:)](https://developer.apple.com/documentation/combine/fail/assign(to:)) is amazing and even manages lifetime. Use when your publishers don't need to change in a model. | |
- Put one `NavigationLink` in your view background to have fully flexible programmatic navigation. You can even build your own navigation stack quite easily. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment