Swiftpack.co - Package - sindresorhus/Defaults
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

404: Not Found

Github

link
Stars: 693

Releases

- 2020-04-18T07:51:05

Important

If you use Swift Package Manager, you need to set the build setting “Other Linker Flags” to -weak_framework Combine to work around this Xcode bug.

Breaking

 extension Defaults.Keys {
-	static let name = OptionalKey<Double>("name")
+	static let name = Key<Double?>("name")
 }

Improvements

Fixes

https://github.com/sindresorhus/Defaults/compare/v3.1.1...v4.0.0

- 2019-10-30T12:49:24

- 2019-09-11T08:07:20

Breaking

Enhancements

- 2019-07-25T01:55:16

- 2018-10-17T10:37:38

Key observation

You can now observe changes to keys in a strongly-typed fashion. It even preserves the type of the oldValue and newValue keys in the change event.

extension Defaults.Keys {
	static let isUnicornMode = Key<Bool>("isUnicornMode", default: false)
}

let observer = defaults.observe(.isUnicornMode) { change in
	// Initial event
	print(change.oldValue)
	//=> false
	print(change.newValue)
	//=> false

	// First actual event
	print(change.oldValue)
	//=> false
	print(change.newValue)
	//=> true
}

defaults[.isUnicornMode] = true

Shorter syntax

 extension Defaults.Keys {
-	static let quality = Defaults.Key<Double>("quality", default: 0.8)
+	static let quality = Key<Double>("quality", default: 0.8)
 }

Support for alternative UserDefaults suites

let extensionDefaults = UserDefaults(suiteName: "com.unicorn.app")!

extension Defaults.Keys {
	static let isUnicorn = Key<Bool>("isUnicorn", default: true, suite: extensionDefaults)
}

defaults[.isUnicorn]
//=> true

// Or

extensionDefaults[.isUnicorn]
//=> true

Registers default values with the native UserDefaults

When you use, for example:

extension Defaults.Keys {
	static let quality = Key<Double>("quality", default: 0.8)
}

It will register 0.8 as the default value with UserDefaults, which can then be used in other contexts, like binding in Interface Builder.