Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

💾 Swifty and modern UserDefaults

License

NotificationsYou must be signed in to change notification settings

sindresorhus/Defaults

Swifty and modernUserDefaults

Store key-value pairs persistently across launches of your app.

It usesUserDefaults underneath but exposes a type-safe facade with lots of nice conveniences.

It's used in production byall my apps (4 million+ users).

Highlights

  • Strongly typed: You declare the type and default value upfront.
  • SwiftUI: Property wrapper that updates the view when theUserDefaults value changes.
  • Codable support: You can store anyCodable value, like an enum.
  • NSSecureCoding support: You can store anyNSSecureCoding value.
  • Observation: Observe changes to keys.
  • Debuggable: The data is stored as JSON-serialized values.
  • Customizable: You can serialize and deserialize your own type in your own way.
  • iCloud support: Automatically synchronize data between devices.

Benefits over@AppStorage

  • You define strongly-typed identifiers in a single place and can use them everywhere.
  • You also define the default values in a single place instead of having to remember what default value you used in other places.
  • You can use it outside of SwiftUI.
  • You can observe value updates.
  • Supports many more types, evenCodable.
  • Easy to add support for your own custom types.
  • Comes with a convenience SwiftUIToggle component.

Compatibility

  • macOS 11+
  • iOS 14+
  • tvOS 14+
  • watchOS 9+
  • visionOS 1+

Install

Addhttps://github.com/sindresorhus/Defaults in the“Swift Package Manager” tab in Xcode.

Documentation

Full documentation

Usage

import DefaultsextensionDefaults.Keys{staticletquality=Key<Double>("quality", default:0.8)}Defaults[.quality]//=> 0.8Defaults[.quality]=0.5//=> 0.5

You can also use it in SwiftUI:

structContentView:View{@Default(.quality)varqualityvarbody:someView{Slider(value: $quality, in:0...1)}}

Maintainers

Related

Contributors18

Languages


[8]ページ先頭

©2009-2025 Movatter.jp