Swiftpack.co - Package - juliand665/UserDefault

Swift Package Manager compatible MIT licensed

UserDefault

An overengineered property wrapper interface for UserDefaults.

This package started out as trying to write a property wrapper for accessing UserDefaults. However, it quickly grew beyond that as I wanted to avoid all the overhead of working with types that aren't directly property list convertible.

Example

Direct Storage

@UserDefault("hasSeenSplashScreen") var hasSeenSplashScreen = false
@UserDefault("name") var name: String?
@UserDefault("count") var count: Int? = 42

hasSeenSplashScreen = true
name = "John Appleseed"
count = nil

Codable/NSCoding

There's a protocol extensions for Codable that lets you avoid having to write that boilerplate yourself:

struct CodableStruct {
	var foo: String
}

extension CodableStruct: Equatable, Codable, DefaultsValueConvertible {}

@UserDefault("test") var test = CodableStruct(foo: "hello, world!")
test.foo = "goodbye."

As well as for NSCoding:

extension UIColor: DefaultsValueConvertible {}
@UserDefault("color") var color = UIColor.systemRed
color = .systemBlue

Further Conditional Conformances

I won't go into detail here, but there are also conditional conformances for Array, Dictionary, Collection, Optional, and RawRepresentable (making it easy to use enums). The goal is to cover all sensible cases in the standard library, so if you find something missing, please open an issue or PR!

Custom Conformance

You can either work directly with plist-compatible values:

struct Convertible {
	var name: String
}

extension Convertible: DefaultsValueConvertible {
	typealias DefaultsRepresentation = String
	
	init(defaultsRepresentation: String) throws {
		name = defaultsRepresentation
	}
	
	func defaultsRepresentation() throws -> String {
		name
	}
}

Or delegate that stuff to another type that conforms to DefaultsValueConvertible:

struct IndirectConvertible {
	var name: String
}

extension IndirectConvertible: IndirectDefaultsValueConvertible {
	typealias DefaultsRepresentation = Convertible.DefaultsRepresentation
	
	init(indirectRepresentation: Convertible) throws {
		name = indirectRepresentation.name
	}
	
	func indirectRepresentation() throws -> Convertible {
		try .init(defaultsRepresentation: name)
	}
}

Protocol Hierarchy

Plist-compatible types (like Bool, Data, String, etc.) conform to the DefaultsValue protocol, which establishes encoding to and from UserDefaults.

The @UserDefault property wrapper works with any type conforming to DefaultsValueConvertible, which encodes to and from an associated type that conforms to DefaultsValue, which is in turn stored in and loaded from the defaults. (DefaultsValue itself also conforms to this protocol so you can use it with the wrapper.)

If you'd like to delegate to another DefaultsValueConvertible type rather than working directly with plist-compatible types, you can instead conform to IndirectDefaultsValueConvertible (which inherits from the former), allowing you to encode to and from any DefaultsValueConvertible type. What this means is that you can have arbitrarily long chains of en-/decoding delegation with minimal boilerplate.

Github

link
Stars: 2
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

v1.0.0 - Sep 23, 2019

This update applies the changes required and/or possible in the final release of Xcode 11 and Swift 5.1.

  • The key no longer has an argument label, since it'll usually be the only argument and its meaning is obvious from the context.
  • Default values are now provided as initial values with = rather than an explicit argument to the wrapper init. (Because I realized I could easily work around the compiler crash preventing me from doing so earlier.)

v0.0.0 - Aug 2, 2019

Use at your own risk!