Slice
and OptionalSlice
are components of the AppState library that allow you to access specific parts of your application’s state. They are useful when you need to manipulate or observe a part of a more complex state structure.
- Slice: Allows you to access and modify a specific part of an existing
State
object. - OptionalSlice: Works similarly to
Slice
but is designed to handle optional values, such as when part of your state may or may not benil
.
- Selective State Access: Access only the part of the state that you need.
- Thread Safety: Just like with other state management types in AppState,
Slice
andOptionalSlice
are thread-safe. - Reactiveness: SwiftUI views update when the slice of the state changes, ensuring your UI remains reactive.
In this example, we use Slice
to access and update a specific part of the state—in this case, the username
from a more complex User
object stored in the app state.
import AppState
import SwiftUI
struct User {
var username: String
var email: String
}
extension Application {
var user: State<User> {
state(initial: User(username: "Guest", email: "guest@example.com"))
}
}
struct SlicingView: View {
@Slice(\.user, \.username) var username: String
var body: some View {
VStack {
Text("Username: \(username)")
Button("Update Username") {
username = "NewUsername"
}
}
}
}
OptionalSlice
is useful when part of your state may be nil
. In this example, the User
object itself may be nil
, so we use OptionalSlice
to safely handle this case.
import AppState
import SwiftUI
extension Application {
var user: State<User?> {
state(initial: nil)
}
}
struct OptionalSlicingView: View {
@OptionalSlice(\.user, \.username) var username: String?
var body: some View {
VStack {
if let username = username {
Text("Username: \(username)")
} else {
Text("No username available")
}
Button("Set Username") {
username = "UpdatedUsername"
}
}
}
}
- Use
Slice
for non-optional state: If your state is guaranteed to be non-optional, useSlice
to access and update it. - Use
OptionalSlice
for optional state: If your state or part of the state is optional, useOptionalSlice
to handle cases where the value may benil
. - Thread Safety: Just like with
State
,Slice
andOptionalSlice
are thread-safe and designed to work with Swift’s concurrency model.
Slice
and OptionalSlice
provide powerful ways to access and modify specific parts of your state in a thread-safe manner. By leveraging these components, you can simplify state management in more complex applications, ensuring your UI stays reactive and up-to-date.