Sometimes Core Data or SQLite are overkill for your app. In those situations, StorageKit is a great solution to save Codable
objects to disk.
StorageKit currently only supports installation via Swift Package Manager.
In Xcode, select File
> Add Packages...
.
Copy and paste the following into the search/input box.
https://github.com/SparrowTek/StorageKit.git
In the options for the StorageKit package, we recommend setting the Dependency Rule to Up to Next Major Version, and enter the
current StorageKit version. Then, click Add Package
.
Select StorageKit
, then click Add Package.
First import StorageKit
import StorageKit
For any Codable
object save and retrieve from disk.
struct User: Codable {
let name: String
let id: Int
}
do {
let user = User(name: "Thomas", id: 123)
try Storage.store(user, to: .documents, as: "user.json")
} catch {
// TODO: handle error
}
let user = Storage.retrieve("user.json", from: .documents, as: User.self)
do {
try Storage.remove("user.json", from: .documents)
} catch {
// TODO: handle error
}
let exists = Storage.fileExists("user.json", in: .documents)