Git Product home page Git Product logo

swift-preference's Introduction

swift-preference

Preference provides property wrapper for UserDefaults and custom Combine publisher that bind a stream for changes.

There are several data types already suppored by default such as Bool, Int, Double, Float, String, URL, Data, Array where Element: PreferenceRepresentable, Dictionary where Key == String, Value: PreferenceRepresentable, Optional where Wrapped: PreferenceRepresentable

Also we have a default implementation of PreferenceRepresentable where Self: RawRepresentable, Self.RawValue: PreferenceRepresentable.

Usage

There are two types String, UserDefaults.FieldKey can be used as UserDefaults key field.

// Use String as key
@Preference("isFlagged") var isFlagged = false

...

extension UserDefaults.FieldKey {
    ...
    
    static let isFlagged: UserDefaults.FieldKey = "isFlagged"
}

// Use UserDefaults.FieldKey as key
@Preference(.isFlagged) var isFlagged = false

Store enum type to UserDefaults

enum Fruit: String, PreferenceRepresentable {
    ...
    case apple
}

@Preference("fruit") var fruit = Fruit.apple

Subscribe value changes for UserDefaults with specified key

@Preference("isFlagged") var isFlagged = false
...

$isFlagged.sink {
    print($0)
}
.store(in: &cancellable)

Installation

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/hdtls/swift-preference", from: "1.0.2")

License

Preference is available under the MIT license. See the LICENSE file for more info.

swift-preference's People

Contributors

hdtls avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.