StorageKit is a Swift library that provides a unified, type-safe interface for storing and retrieving data across multiple backends. It supports declarative property wrappers and integrates with Combine for reactive state updates. Ideal for modern app architectures requiring persistent, transient, or secure storage.
- Type-safe read/write access to stored values
- Reactive integration with Combine for observing value changes
- Support for multiple storage backends:
- UserDefaults
- File system
- Keychain
- In-memory
- Property wrappers for declarative usage
- Support for composable storage layers
- Built-in expiration handling for time-sensitive values
Add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/NikSativa/StorageKit.git", from: "1.0.0")
]
Property wrapper that provides type-safe access to values persisted in UserDefaults. Supports Codable types:
import StorageKit
struct User: Codable {
let name: String
let email: String
let age: Int
}
final class UserViewModel {
@Defaults("user", defaultValue: nil)
var user: User? {
didSet {
print("User updated: \(user)")
}
}
}
Property wrapper that adds expiration to stored values:
@Expirable(lifetime: .oneHour) var token: String?
Persistent storage using UserDefaults:
let storage = UserDefaultsStorage<Int?>(key: "MyKey")
storage.value = 1
Persistent storage using the file system. Supports customizable file paths:
let storage = FileStorage<Int>(fileName: "TestFile.txt")
storage.value = 1
Secure storage using the Keychain:
let storage = KeychainStorage(
key: "MyKey",
configuration: .init(service: Bundle.main.bundleIdentifier ?? "MyService")
)
storage.value = auth.token
Transient storage using in-memory values:
let storage = InMemoryStorage<Int>(value: 1)
storage.value = 1
Combine multiple storage layers for advanced scenarios:
// Combine two storages
let combined = inMemoryStorage.combine(userDefaultsStorage)
// Combine multiple storages (iOS 16+)
let combined = zip(storages: [
InMemoryStorage<Int?>(value: 1),
UserDefaultsStorage(key: "MyKey")
])
All storage types provide Combine publishers for observing value changes:
let storage = UserDefaultsStorage<String>(key: "MyKey")
let cancellable = storage.sink { value in
print("Value updated: \(value)")
}
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
- Swift 5.5+
- Xcode 13.0+
StorageKit is available under the MIT license. See the LICENSE file for more info.