https://github.com/niksativa/storagekit
Swift library for saving and retrieving data from any kind storage
https://github.com/niksativa/storagekit
file inmemory ios keychain propertywrapper storage swift userdefaults wrapper
Last synced: about 1 year ago
JSON representation
Swift library for saving and retrieving data from any kind storage
- Host: GitHub
- URL: https://github.com/niksativa/storagekit
- Owner: NikSativa
- License: mit
- Created: 2024-04-02T15:01:26.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-08T23:13:19.000Z (over 1 year ago)
- Last Synced: 2025-04-15T08:16:04.870Z (about 1 year ago)
- Topics: file, inmemory, ios, keychain, propertywrapper, storage, swift, userdefaults, wrapper
- Language: Swift
- Homepage:
- Size: 26.4 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StorageKit
[](https://swiftpackageindex.com/NikSativa/StorageKit)
[](https://swiftpackageindex.com/NikSativa/StorageKit)
Swift library for saving and retrieving data from any kind storage.
### Defaults
Wrapper for UserDefaults that allows you to store and retrieve Codable objects.
```swift
import FoundationKit
struct User: Codable {
let name: String
let email: String
let age: Int
}
final class UserViewModel {
@Defaults("user", defaultValue: nil)
var user: User? {
didSet {
print("new user: \(user)")
}
}
}
```
## UserDefaultsStorage
A storage that provides methods to save and retrieve data from UserDefaults.
```swift
let storage = UserDefaultsStorage(key: "MyKey")
storage.value = 1
```
## FileStorage
A storage that provides methods to save and retrieve data from file system. It uses `FileManager` to interact with file system.
Default path mask is: ./*userDomainMask*/*cachesDirectory*/**Storages**/*fileName*.stg
```swift
let storage = FileStorage(fileName: "TestFile.txt")
storage.value = 1
```
## InMemoryStorage
A storage that provides methods to save and retrieve data in memory.
```swift
let storage = InMemoryStorage(value: 1)
storage.value = 1
```
## KeychainStorage
A storage that provides methods to save and retrieve data from OS Keychain.
Most safety storage, but with limitations by [SDK](https://developer.apple.com/documentation/security).
```swift
let storage = KeychainStorage(key: "MyKey", configuration: .init(service: Bundle.main.bundleIdentifier ?? "MyService")
storage.value = auth.token
```
## AnyStorage
Type-erased storage that provides methods to save and retrieve data from any kind storage. Each storage has `toAny()` method which is used to convert specific storage to `AnyStorage`.
```swift
let storage = UserDefaultsStorage(key: "MyKey").toAny()
storage.value = 1
```
## Composition
`AnyStorage` conforms to `Storage` protocol and can be used in composition with other storages by method `combine()` or global function `zip(storages:)`
```swift
let userDefaultsStorage = UserDefaultsStorage(value: 1)
let inMemoryStorage = InMemoryStorage(value: 1)
let combined = inMemoryStorage.combine(userDefaultsStorage) // AnyStorage
combined.value = 1
```
`zip(storages: [any Storage])` is only available in iOS 16 or newer
```swift
let combined = zip(storages: [
InMemoryStorage(value: 1),
UserDefaultsStorage(key: "MyKey")
])
```
`zip(storages: [AnyStorage])` is deprecated in iOS 16 or newer
```swift
let combined = zip(storages: [
InMemoryStorage(value: 1).toAny(),
UserDefaultsStorage(key: "MyKey").toAny()
])
```
### Expirable
Property wrapper that allows you to set expiration time for the value.
```swift
@Expirable(lifetime: .oneHour) var token: String?
```