@@ -29,10 +29,6 @@ SwiftUI, UIKit, AppKit, and even non-Apple platforms.
29
29
30
30
#### SwiftUI
31
31
32
- > [ !IMPORTANT]
33
- > To get access to the tools described below you must depend on the SwiftNavigation package and
34
- > import the SwiftUINavigation library.
35
-
36
32
SwiftUI already comes with incredibly powerful navigation APIs, but there are a few areas lacking
37
33
that can be filled. In particular, driving navigation from enum state so that you can have
38
34
compile-time guarantees that only one destination can be active at a time.
@@ -96,10 +92,16 @@ This is more concise, and we get compile-time verification that at most one dest
96
92
active at a time. However, SwiftUI does not come with the tools to drive navigation from this
97
93
model. This is where the SwiftUINavigation tools becomes useful.
98
94
95
+ > [ !IMPORTANT]
96
+ > To get access to the tools described below you must depend on the SwiftNavigation package
97
+ > (see [ Installation] ( #installation ) ) and import the ** SwiftUINavigation** library.
98
+
99
99
We start by annotating the ` Destination ` enum with the ` @CasePathable ` macro, which allows one to
100
100
refer to the cases of an enum with dot-syntax just like one does with structs and properties:
101
101
102
102
``` diff
103
+ + import SwiftNavigation
104
+
103
105
+ @CasePathable
104
106
enum Destination {
105
107
// ...
@@ -110,6 +112,8 @@ And now one can use simple dot-chaining syntax to derive a binding from a partic
110
112
the ` destination ` property:
111
113
112
114
``` swift
115
+ import SwiftUINavigation
116
+ // ...
113
117
.sheet (item : $model.destination .addItem ) { addItemModel in
114
118
AddItemView (model : addItemModel)
115
119
}
@@ -133,7 +137,7 @@ we can still use SwiftUI's navigation APIs.
133
137
134
138
> [ !IMPORTANT]
135
139
> To get access to the tools described below you must depend on the SwiftNavigation package and
136
- > import the UIKitNavigation library.
140
+ > import the ** UIKitNavigation** library.
137
141
138
142
Unlike SwiftUI, UIKit does not come with state-driven navigation tools. Its navigation tools are
139
143
"fire-and-forget", meaning you simply invoke a method to trigger a navigation, but there is
@@ -151,17 +155,20 @@ This makes it easy to get started with navigation, but as SwiftUI has taught us,
151
155
powerful to be able to drive navigation from state. It allows you to encapsulate more of your
152
156
feature's logic in an isolated and testable domain, and it unlocks deep linking for free since one
153
157
just needs to construct a piece of state that represents where you want to navigate to, hand it to
154
- SwiftUI, and let SwiftUI handle the rest.
158
+ SwiftUI, and let it handle the rest.
155
159
156
160
The UIKitNavigation library brings a powerful suite of navigation tools to UIKit that are heavily
157
161
inspired by SwiftUI. For example, if you have a feature model like the one discussed above in
158
162
the [ SwiftUI] ( #swiftui ) section:
159
163
160
164
``` swift
165
+ import SwiftNavigation
166
+
161
167
@Observable
162
168
class FeatureModel {
163
169
var destination: Destination?
164
170
171
+ @CasePathable
165
172
enum Destination {
166
173
case addItem (AddItemModel)
167
174
case deleteItemAlert
@@ -173,6 +180,8 @@ class FeatureModel {
173
180
…then one can drive navigation in a _ view controller_ using tools in the library:
174
181
175
182
``` swift
183
+ import UIKitNavigation
184
+
176
185
class FeatureViewController : UIViewController {
177
186
@UIBindable var model: FeatureModel
178
187
0 commit comments