Constant
in the AppState library provides read-only access to values within your application's state. It works similarly to Slice
, but ensures that the accessed values are immutable. This makes Constant
ideal for accessing values that may otherwise be mutable but should remain read-only in certain contexts.
- Read-Only Access: Constants provide access to mutable state, but the values cannot be modified.
- Scoped to Application: Like
Slice
,Constant
is defined within theApplication
extension and scoped to access specific parts of the state. - Thread-Safe:
Constant
ensures safe access to state in concurrent environments.
Here’s how you define a Constant
in the Application
extension to access a read-only value:
import AppState
import SwiftUI
struct ExampleValue {
var username: String?
var isLoading: Bool
let value: String
var mutableValue: String
}
extension Application {
var exampleValue: State<ExampleValue> {
state(
initial: ExampleValue(
username: "Leif",
isLoading: false,
value: "value",
mutableValue: ""
)
)
}
}
In a SwiftUI view, you can use the @Constant
property wrapper to access the constant state in a read-only manner:
import AppState
import SwiftUI
struct ExampleView: View {
@Constant(\.exampleValue, \.value) var constantValue: String
var body: some View {
Text("Constant Value: \(constantValue)")
}
}
Even if the value is mutable elsewhere, when accessed through @Constant
, the value becomes immutable:
import AppState
import SwiftUI
struct ExampleView: View {
@Constant(\.exampleValue, \.mutableValue) var constantMutableValue: String
var body: some View {
Text("Read-Only Mutable Value: \(constantMutableValue)")
}
}
- Use for Read-Only Access: Use
Constant
to access parts of the state that should not be modified within certain contexts, even if they are mutable elsewhere. - Thread-Safe: Like other AppState components,
Constant
ensures thread-safe access to state. - Use
OptionalConstant
for Optional Values: If the part of the state you're accessing may benil
, useOptionalConstant
to safely handle the absence of a value.
Constant
and OptionalConstant
provide an efficient way to access specific parts of your app's state in a read-only manner. They ensure that values which may otherwise be mutable are treated as immutable when accessed within a view, ensuring safety and clarity in your code.