Swiftpack.co - HUK-COBURG/schwifty-resources as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by HUK-COBURG.
HUK-COBURG/schwifty-resources 2.2.1
A schwifty way of reading and writing data.
⭐️ 2
🕓 11 weeks ago
iOS macOS
.package(url: "https://github.com/HUK-COBURG/schwifty-resources.git", from: "2.2.1")
FoundationDB logo


With schwifty-resources you get a schwifty way of reading and writing data.

The main idea is to have a similar way of accessing local or remote resources, convenient to use, although having a high flexibility. It can be used for communicating over HTTP, reading or writing files in the sandbox, acessing user defaults and bundle resources.

Getting Started Using schwifty-resources

Adding the package

In your Package.swift Swift Package Manager manifest, add the following dependency to your dependencies argument:

.package(url: "https://github.com/HUK-COBURG/schwifty-resources.git", .branch("main")),

Add the dependency to any targets you've declared in your manifest:

.target(name: "MyTarget", dependencies: ["SchwiftyResources"]),

Reading bundle resources

To get some data in the bundle, use BundleResource, supplying a ContentResourceDecoder for the type of data you want to read and a file name to identify it. In this example a StringResourceCoder is used to get the content of the file document.txt in the main bundle as a String.

import SchwiftyResources

private struct DocumentBundleResource: BundleResource {
    typealias ContentResourceDecoder = StringResourceCoder
    let fileName: String = "document.txt"

do {
    let resource = DocumentBundleResource()
    let content = try await resource.read()
} catch {

Getting HTTP API content

To get some data from an HTTP API, use HttpResource, supplying a RequestBodyResourceEncoder, ResponseBodyResourceDecoder and an URL. In this example the request body will be empty, we are expecting a JSON response body from the url https://jsonplaceholder.typicode.com/todos with Todo instances in it.

import SchwiftyResources
import Foundation

private struct Todo: Codable {
    let userId: Int
    let id: Int
    let title: String
    let completed: Bool

private struct TodosHttpResource: HttpResource {
    typealias RequestBodyResourceEncoder = EmptyResourceCoder
    typealias ResponseBodyResourceDecoder = JsonResourceCoder<[Todo]>
    let url: URL = "https://jsonplaceholder.typicode.com/todos"

do {
    let resource = TodosHttpResource()
    let response = try await resource.response
    let content = response.content
} catch {

Writing and reading to and from the user defaults

To write data to and read from the user defaults you can create a UserDefaultsResource, defining the ContentResourceCoder and a key.

import SchwiftyResources

struct PropertyUserDefaultsResource: UserDefaultsResource {
    typealias ContentResourceCoder = StringResourceCoder
    let key: String = "property"

let resource = PropertyUserDefaultsResource()

do {
    try resource.write(content: "Just a simple string.")
} catch {

do {
    let value = try resource.read()
} catch {

Writing and reading to and from the sandbox

To write data to and read from the sandbox you can create a SandboxResource, defining the ContentResourceCoder, a location and the path (or just the file name). In this example the data, which is stored on the file system will be AES GCM encrypted. The decrypted data though contains an encoded version of the Rick type.

import SchwiftyResources
import Foundation
import CryptoKit

struct Rick: Codable {
    let identifier: String
    let haircut: String

struct RicksKeyProvider: AesGcmCrypterKeyProvider {
    static func provideKey() -> SymmetricKey {
        let password = "pAssW0rd#OF-the/c1Tad3l"
        return SymmetricKey(data: SHA256.hash(data: Data(password.utf8)))

struct RicksSandboxResource: SandboxResource {
    typealias ContentResourceCoder = CryptedJsonResourceCoder<[Rick], AesGcmCrypter<RicksKeyProvider>>
    let location: SandboxLocation = .documents
    var path: String = "ricks.store"

let resource = RicksSandboxResource()

do {
    let ricks: [Rick] = [Rick(identifier: "C-137", haircut: "Mad scientist"),
                         Rick(identifier: "Rick Prime", haircut: "Mad scientist (short)")]
    try await resource.write(content: ricks)
} catch {

do {
    let ricks = try await resource.read()
} catch {

Why the name?

We like the TV show Rick and Morty, schwifty is a reference to a song of them and it kind of sounds like Swift - simple as that.



Stars: 2
Last commit: 1 week ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

11 weeks ago

This release fixes an issue where URLs were not built correctly, when percent escaped characters were in the query.

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