Skip to content

Releases: sindresorhus/Defaults

v3.0.0

11 Sep 08:07
Compare
Choose a tag to compare

Breaking

  • Require Xcode 11 and Swift 5.1 for building 90ac6f8
  • Switch from defaults to Defaults 90ac6f8
    Example: defaults[.unicorn]Defaults[.unicorn]
    Example: defaults.observableDefaults.observable
  • Rename defaults.clear to Defaults.removeAll and make it a static method 27c9997

Enhancements

  • Add .reset() method to reset the given keys back to their default value d1e4215

v2.0.2

25 Jul 01:55
Compare
Choose a tag to compare
  • Fix the source file location for SwiftPM 674fde4

v2.0.1

08 Jul 07:09
Compare
Choose a tag to compare
  • Fix Swift 5 upgrade warning 865a4b0

v2.0.0

05 Apr 07:30
Compare
Choose a tag to compare
  • Upgrade to and require Swift 5. 908d3b4

v1.0.0

17 Oct 10:37
Compare
Choose a tag to compare

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.