Swiftpack.co - Misoservices/MisoDefaultsWrapper as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Misoservices/MisoDefaultsWrapper
A lightweight UserDefaults Swift wrapper
.package(url: "https://github.com/Misoservices/MisoDefaultsWrapper.git", from: "1.0.1")

MisoDefaultsWrapper

A lightweight UserDefaults Swift wrapper

The goal of this yet-another defaults wrapper is to provide a very simple wrapper for any given data. The use-case being the preferences, as set by an application.

It is not meant as a full synchronization system between multiple applications. As such, it reads individual objects on first request, and keeps them in memory. It then saves the data back to UserDefaults on write.

Compared to other UserDefaults wrapper, the MisoDefaultsWrapper doesn't try to be fancy or all-encompassing. As such, there are two ways to create a key: through the key: parameter or the jsonKey: parameter. For simple values that are known to the UserDefaults system, the key: parameter is perfect. For more complex objects, including optionals, the jsonKey: parameter works its magic, as long as you implement your class or struct as Codable.

Since the data is kept in-memory, there is no registration involved either.

Published object

An interesting part of the Published interface is knowing whenever an object has changed internally. If you want, you can make your object both ObservableObject and Codable to reap the rewards.

Usage

Storing a simple key

import MisoDefaultsWrapper

@Defaults(key: "AFunKey")
public var mySimpleObject = "A Simple Text"

Storing a key in json

As bonus, you can use optionals in json

import MisoDefaultsWrapper

@Defaults(jsonKey: "ABiggerObject")
public var mySimpleObject: MyCodableObject? = nil

Using Combine to publish a simple key

import MisoDefaultsWrapper

@Published(key: "activationRequest")
public var activationRequest: Bool = false

Using Combine to publish an observable key

import MisoDefaultsWrapper

@Published(jsonKey: "somethingBig")
public var somethingBig = MyObservableObject()

Disallow saving into a particular User Default

import MisoDefaultsWrapper

// Disllow saving until we know we can
MisoDefaultsWrapper.lockedUserDefaults.insert(.standard)

// Allow saving
MisoDefaultsWrapper.lockedUserDefaults.remove(.standard)

Colophon

The official address for this package

The git / package url

This package is created and maintained by Misoservices Inc. and is licensed under the BSL-1.0: Boost Software License - Version 1.0.

GitHub

link
Stars: 3
Last commit: 2 weeks 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.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Increased minimal package version so they compile
1 year ago

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