SwiftStorage is an easy way to persist data without Key Value. And it is designed to integrate seamlessly with SwiftUI.
I would like the framework to have the following features
- Support for Observation framework
- Can be disabled on a per-property basis
- Save in UserDefaults
- Save in Key Value Store
- Specify keys individually
- Key Value Store and UserDefaults together Those not checked are to be realized in the future. However, we do not know when this will be, as our vision for implementation is not set in stone.
Basically, the usage is the same as the Observable macro in the Observation framework. Use the Storage macro instead of the Observable macro. This change alone allows you to store properties permanently.
import SwiftStorage
@Storage
final class SwiftStorageModel {
var storedValue: Bool
init() {
self.storedValue = false
}
}
import SwiftUI
struct SwiftStorageView: View {
@State private var swiftStorage = SwiftStorageModel()
var body: some View {
NavigationStack {
Form {
Toggle("Bool", isOn: $swiftStorage.storedValue)
}
}
}
}
If it is a constant, it will not be saved. Also, if the Transient macro is applied, even if it is a variable, it will not be saved.
import SwiftStorage
@Storage
final class SwiftStorageModel {
var storedValue: Bool
@Transient
var temporaryValue: String
let constantValue: String
init() {
self.storedValue = false
self.temporaryValue = ""
self.constantValue = ""
}
}
You can add it to your project using the Swift Package Manager To add SwiftStorage to your Xcode project, select File > Add Package Dependancies... and find the repository URL:
https://github.com/KC-2001MS/SwiftStorage.git
.
See CONTRIBUTING.md if you want to make a contribution.
Documentation on the SwiftStorage framework can be found here.
This library is released under Apache-2.0 license. See LICENSE for details.
If you would like to make a donation to this project, please click here. The money you give will be used to improve my programming skills and maintain the application.
Pay by PayPal