目录
dependabot[bot]

Bump danger from 9.3.0 to 9.3.1 (#79)

Bumps danger from 9.3.0 to 9.3.1.

Release notes

Sourced from danger's releases.

9.3.1

Changelog

Sourced from danger's changelog.

9.3.1

Commits

Dependabot compatibility
score

Dependabot will resolve any conflicts with this PR as long as you don’t alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options

You can trigger Dependabot actions by commenting on this PR:

  • @dependabot rebase will rebase this PR
  • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
  • @dependabot merge will merge this PR after your CI passes on it
  • @dependabot squash and merge will squash and merge this PR after your CI passes on it
  • @dependabot cancel merge will cancel a previously requested merge and block automerging
  • @dependabot reopen will reopen this PR if it is closed
  • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
  • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)

Signed-off-by: dependabot[bot] support@github.com Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

1年前145次提交
目录README.md

Foil Actions Status

A lightweight property wrapper for UserDefaults done right


About

Read the post: A better approach to writing a UserDefaults Property Wrapper

Why the name?

Foil, as in “let me quickly and easily wrap and store this leftover food in some foil so I can eat it later.” 🌯 😉

Foil:
noun
North America
A very thin, pliable, easily torn sheet of aluminum used for cooking, packaging, cosmetics, and insulation.

Usage

You can use @WrappedDefault for non-optional values and @WrappedDefaultOptional for optional ones. You may wish to store all your user defaults in one place, however, that is not necessary. Any property on any type can use this wrapper.

final class AppSettings {
    static let shared = AppSettings()

    @WrappedDefault(key: "flagEnabled")
    var flagEnabled = true

    @WrappedDefault(key: "totalCount")
    var totalCount = 0

    @WrappedDefaultOptional(key: "timestamp")
    var timestamp: Date?
}

// Usage

func userDidToggleSetting(_ sender: UISwitch) {
    AppSettings.shared.flagEnabled = sender.isOn
}

There is also an included example app project.

Using enum keys

If you prefer using an enum for the keys, writing an extension specific to your app is easy. However, this is not required. In fact, unless you have a specific reason to reference the keys, this is completely unnecessary.

enum AppSettingsKey: String, CaseIterable {
    case flagEnabled
    case totalCount
    case timestamp
}

extension WrappedDefault {
    init(wrappedValue: T, _ key: AppSettingsKey) {
        self.init(wrappedValue: wrappedValue, key: key.rawValue)
    }
}

extension WrappedDefaultOptional {
    init(_ key: AppSettingsKey) {
        self.init(key: key.rawValue)
    }
}

Observing changes

There are many ways to observe property changes. The most common are by using Key-Value Observing or a Combine Publisher. KVO observing requires the object with the property to inherit from NSObject and the property must be declared as @objc dynamic.

final class AppSettings: NSObject {
    static let shared = AppSettings()

    @WrappedDefaultOptional(key: "userId")
    @objc dynamic var userId: String?

    @WrappedDefaultOptional(key: "average")
    var average: Double?
}

Using KVO

let observer = AppSettings.shared.observe(\.userId, options: [.new]) { settings, change in
    print(change)
}

Using Combine

Note: that average does not need the @objc dynamic annotation, .receiveValue will fire immediately with the current value of average and on every change after.

AppSettings.shared.$average
    .sink {
        print($0)
    }
    .store(in: &cancellable)

Combine Alternative with KVO

Note: in this case, userId needs the @objc dynamic annotation and AppSettings needs to inherit from NSObject. Then receiveValue will fire only on changes to wrapped object’s value. It will not publish the initial value as in the example above.

AppSettings.shared
    .publisher(for: \.userId, options: [.new])
    .sink {
        print($0)
    }
    .store(in: &cancellable)

Supported types

The following types are supported by default for use with @WrappedDefault.

Adding support for custom types is possible by conforming to UserDefaultsSerializable. However, this is highly discouraged. UserDefaults is not intended for storing complex data structures and object graphs. You should probably be using a proper database (or serializing to disk via Codable) instead.

  • Bool
  • Int
  • UInt
  • Float
  • Double
  • String
  • URL
  • Date
  • Data
  • Array
  • Set
  • Dictionary
  • RawRepresentable types

Additional Resources

Supported Platforms

  • iOS 13.0+
  • tvOS 13.0+
  • watchOS 6.0+
  • macOS 11+

Requirements

Installation

CocoaPods

pod 'Foil', '~> 4.0.0'

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/jessesquires/Foil.git", from: "4.0.0")
]

Alternatively, you can add the package directly via Xcode.

Documentation

You can read the documentation here. Generated with jazzy. Hosted by GitHub Pages.

Contributing

Interested in making contributions to this project? Please review the guides below.

Also consider sponsoring this project or buying my apps! ✌️

Credits

Created and maintained by Jesse Squires.

License

Released under the MIT License. See LICENSE for details.

Copyright © 2021-present Jesse Squires.

邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号