đź§ Effortless State Management and Dependency Injection for Swift 6 Applications
AppState is a Swift 6 library designed to simplify the management of application state in a thread-safe, type-safe, and SwiftUI-friendly way. It provides a set of tools to centralize and synchronize state across your application, as well as inject dependencies into various parts of your app.
Non-Apple Platform Support: Linux & Windows
🍎 Features marked with this symbol are specific to Apple platforms, as they rely on Apple technologies such as iCloud and the Keychain.
AppState includes several powerful features to help manage state and dependencies:
UserDefaults
, ideal for saving small amounts of data between app launches.FileManager
, useful for storing larger amounts of data securely on disk.ObservableObject
dependencies so your views update when they change.To integrate AppState into your Swift project, you’ll need to use the Swift Package Manager. Follow the Installation Guide for detailed instructions on setting up AppState.
After installation, refer to the Usage Overview for a quick introduction on how to manage state and inject dependencies into your project.
Below is a minimal example showing how to define a piece of state and access it from a SwiftUI view:
import AppState
import SwiftUI
private extension Application {
var counter: State<Int> {
state(initial: 0)
}
}
struct ContentView: View {
@AppState(\.counter) var counter: Int
var body: some View {
VStack {
Text("Count: \(counter)")
Button("Increment") { counter += 1 }
}
}
}
This snippet demonstrates defining a state value in an Application
extension and using the @AppState
property wrapper to bind it inside a view.
Here’s a detailed breakdown of AppState’s documentation:
StoredState
.ObservableObject
dependencies in your views.We welcome contributions! Please check out our Contributing Guide for how to get involved.
With AppState installed, you can start exploring its key features by checking out the Usage Overview and more detailed guides. Get started with managing state and dependencies effectively in your Swift projects! For more advanced usage techniques, like Just-In-Time creation and preloading dependencies, see the Advanced Usage Guide. You can also review the Constant and ObservedDependency guides for additional features.