项目
在 GitHub 查看 Benefits over
sindresorhus/Defaults
关于
💾 Swift 风格且现代的 UserDefaults
系统与基础设施可以做什么
- Strongly typed: You declare the type and default value upfront.
- SwiftUI: Property wrapper that updates the view when the UserDefaults value changes.
- Codable support: You can store any Codable value, like an enum.
- NSSecureCoding support: You can store any NSSecureCoding value.
实测星标增长
- 24小时
- 0
- 7天
- -2
- 15天
- -4
- 1个月
- -2
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Defaults
Swifty and modern UserDefaults
Store key-value pairs persistently across launches of your app.
It uses UserDefaults underneath but exposes a type-safe facade with lots of nice conveniences.
It's used in production by all my apps (4 million+ users).
Highlights
- Strongly typed: You declare the type and default value upfront.
- SwiftUI: Property wrapper that updates the view when the
UserDefaultsvalue changes. - Codable support: You can store any Codable value, like an enum.
- NSSecureCoding support: You can store any NSSecureCoding 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, even
Codable. - Easy to add support for your own custom types.
- Comes with a convenience SwiftUI
Togglecomponent.
Compatibility
- macOS 11+
- iOS 14+
- tvOS 14+
- watchOS 9+
- visionOS 1+
Install
Add https://github.com/sindresorhus/Defaults in the “Swift Package Manager” tab in Xcode.
Documentation
Usage
import Defaults
extension Defaults.Keys {
static let quality = Key<Double>("quality", default: 0.8)
}
Defaults[.quality]
//=> 0.8
Defaults[.quality] = 0.5
//=> 0.5
You can also use it in SwiftUI:
struct ContentView: View {
@Default(.quality) var quality
var body: some View {
Slider(value: $quality, in: 0...1)
}
}
Maintainers
Related
- KeyboardShortcuts - Add user-customizable global keyboard shortcuts to your macOS app
- LaunchAtLogin - Add "Launch at Login" functionality to your macOS app
- DockProgress - Show progress in your app's Dock icon
- Gifski - Convert videos to high-quality GIFs on your Mac
- More…
探索相关项目
加载中…