Slice and OptionalSlice Usage
August 26, 2025 · View on GitHub
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.
Overview
- Slice: Allows you to access and modify a specific part of an existing
Stateobject. - OptionalSlice: Works similarly to
Slicebut is designed to handle optional values, such as when part of your state may or may not benil.
Key Features
- Selective State Access: Access only the part of the state that you need.
- Thread Safety: Just like with other state management types in AppState,
SliceandOptionalSliceare thread-safe. - Reactiveness: SwiftUI views update when the slice of the state changes, ensuring your UI remains reactive.
Example Usage
Using Slice
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"
}
}
}
}
Using OptionalSlice
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"
}
}
}
}
Best Practices
- Use
Slicefor non-optional state: If your state is guaranteed to be non-optional, useSliceto access and update it. - Use
OptionalSlicefor optional state: If your state or part of the state is optional, useOptionalSliceto handle cases where the value may benil. - Thread Safety: Just like with
State,SliceandOptionalSliceare thread-safe and designed to work with Swift’s concurrency model.
Conclusion
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.