A powerful SwiftUI property wrapper that simplifies persistent storage using UserDefaults
. With built-in JSON support, StikAppStorage
allows you to store both simple types (String
, Int
, Bool
, etc.) and custom Codable
types seamlessly.
- Simplified Persistent Storage: Works just like
AppStorage
but with added functionality. - JSON Support: Store custom
Codable
types, like structs and enums. - Fallback Defaults: Provides a fallback value if no data exists or decoding fails.
- SwiftUI Integration: Fully compatible with SwiftUI's reactive system.
-
In Xcode, go to File → Swift Packages → Add Package Dependency.
-
Enter the repository URL:
https://github.com/0-Blu/StikAppStorage
-
Select the latest version and add the package to your project.
Easily store and retrieve simple types, like String
, Int
, or Bool
:
import StikAppStorage
import SwiftUI
struct ContentView: View {
@StikAppStorage("username") var username = "Guest"
@StikAppStorage("isDarkMode") var isDarkMode = false
var body: some View {
VStack {
Text("Hello, \(username)!")
Toggle("Dark Mode", isOn: $isDarkMode)
TextField("Enter your name", text: $username)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
}
.preferredColorScheme(isDarkMode ? .dark : .light)
.padding()
}
}
You can store any custom type that conforms to Codable
. Here's an example with a UserProfile
struct:
import StikAppStorage
import SwiftUI
struct UserProfile: Codable {
var name: String
var age: Int
}
struct ContentView: View {
@StikAppStorage("userProfile") private var userProfile = UserProfile(name: "John Doe", age: 25)
var body: some View {
VStack {
Text("Name: \(userProfile.name)")
Text("Age: \(userProfile.age)")
Button("Update Profile") {
$userProfile.wrappedValue = UserProfile(name: "Jane Doe", age: 30)
}
}
.padding()
}
}
If no data exists for a given key, the default value provided in @StikAppStorage
is used:
@StikAppStorage("launchCount") var launchCount = 0
@StikAppStorage("isLoggedIn") var isLoggedIn = false
- Simple Types: Stored directly into
UserDefaults
. - Custom Types: Encoded into JSON using
JSONEncoder
and saved asData
inUserDefaults
. - Decoding: Automatically decoded back into the original type using
JSONDecoder
.
StikAppStorage
is available under the MIT License. See the LICENSE file for more details.