Swiftpack.co -  lucamegh/Storage as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Functional storage abstraction layer.
.package(url: "https://github.com/lucamegh/Storage.git", from: "1.0.0")

Storage 🧳

When it comes to persisting data in our apps, we have plenty of choice: UserDefaults, NSUbiquitousKeyValueStore, Core Data, disk, you name it. What we are often missing is a common abstraction layer between all these storage types. Enter Storage, the functional storage abstraction layer.

struct Storage<Value> {
    var store: (Value?) -> Void
    var restore: () -> Value?


Storage is distributed using Swift Package Manager. To install it into a project, simply add it as a dependency within your Package.swift manifest:

let package = Package(
    dependencies: [
        .package(url: "https://github.com/lucamegh/Storage", from: "1.0.0")


Storage comes with different built-in storages to cover the most common use cases.

To store a value in UserDefaults.standard create a storage using the Stoarge.userDefaults static factory method:

let storage = Storage<User>.userDefaults(key: "logged-user-key")

You can optionally provide your own UserDefaults, or customize how your models gets encoded and decoded.

Other default storages include disk and ubiquitousKeyValueStore.

Use high-order storages to adjust storing/restoring behavior.

var weatherStorage: Storage<WeatherReport> = ...
weatherStorage = storage.withConditionalRestore { report in
    let oneHourAgo = Calendar.current.date(byAdding: .hour, value: -1, to: Date())!
    return report.timestamp > oneHourAgo
weatherStorage.restore() // nil if weather report is older than an hour

Storage comes with a convenient property wrapper to simplify working with storages even further:

@Stored(storage: .userDefaults(key: "isFirstLaunch") var isFirstLaunch = true

Since every storage is an instance of the very same Storage<Value> type, you can swap storages in-line to make your tests easier to write:

let viewModel = UserViewModel(
    userStorage: Storage(
        restore: { User(firstName: "John", lastName: "Appleseed") },
        store: { _ in fatalError() }
XCTAssertEqual(viewModel.fullName, "John Appleseed")


Stars: 0
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Storage 1.0.0
2 weeks ago

The first release of Storage.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API