Skip to content

@sindresorhus sindresorhus released this Sep 11, 2019 · 1 commit to master since this release

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
Assets 2

@sindresorhus sindresorhus released this Jul 25, 2019 · 7 commits to master since this release

  • Fix the source file location for SwiftPM 674fde4
Assets 2

@sindresorhus sindresorhus released this Jul 8, 2019 · 9 commits to master since this release

  • Fix Swift 5 upgrade warning 865a4b0
Assets 2

@sindresorhus sindresorhus released this Apr 5, 2019 · 14 commits to master since this release

  • Upgrade to and require Swift 5. 908d3b4
Assets 2

@sindresorhus sindresorhus released this Oct 17, 2018 · 17 commits to master since this release

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.

Assets 2
You can’t perform that action at this time.