StorageKit

1.1.3

Swift library for saving and retrieving data from any kind storage
NikSativa/StorageKit

What's New

1.1.3

2024-04-17T23:39:11Z

Full Changelog: 1.1.2...1.1.3

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.

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.

let storage = UserDefaultsStorage<Int?>(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

let storage = FileStorage<Int>(fileName: "TestFile.txt")
storage.value = 1

InMemoryStorage

A storage that provides methods to save and retrieve data in memory.

let storage = InMemoryStorage<Int>(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.

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.

let storage = UserDefaultsStorage<Int?>(key: "MyKey").toAny()
storage.value = 1

Composition

AnyStorage<Value> conforms to Storage protocol and can be used in composition with other storages by method combine() or global function zip(storages:)

let userDefaultsStorage = UserDefaultsStorage<Int?>(value: 1)
let inMemoryStorage = InMemoryStorage<Int>(value: 1)

let combined = inMemoryStorage.combine(userDefaultsStorage) // AnyStorage<Int>
combined.value = 1

zip<Value>(storages: [any Storage<Value>]) is only available in iOS 16 or newer

let combined = zip(storages: [
    InMemoryStorage<Int?>(value: 1),
    UserDefaultsStorage(key: "MyKey")
])

zip<Value>(storages: [AnyStorage<Value>]) is deprecated in iOS 16 or newer

let combined = zip(storages: [
    InMemoryStorage<Int?>(value: 1).toAny(),
    UserDefaultsStorage(key: "MyKey").toAny()
])

Expirable

Property wrapper that allows you to set expiration time for the value.

@Expirable(lifetime: .oneHour) var token: String?

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 20:12:33 GMT-0900 (Hawaii-Aleutian Daylight Time)