Coder Social home page Coder Social logo

userdefault's Introduction

UserDefault

UserDefault wrapper enables you to use UserDefaults in the simplest way with the power of @propertyWrapper.

Primitive types of Swift which conforms Codable protocol internally are supported. (String, Int, Float, Double, Data, Bool, Date, URL)

Installation

Swift Package Manager:

To integrate using Apple's Swift package manager, add the following as a dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/strawb3rryx7/UserDefault.git", from: "master")
]

Manually

Just drag the UserDefault.swift file into your project directory. It's all done.

Usage

import UserDefault

struct Defaults {
    @UserDefault("is_discount_provided", defaultValue: false)
    static var isDiscountProvided: Bool
}

How to Modify?

Well, that's pretty simple. You only have to access the variable through the Defaults struct, and set it the value.

Defaults.isDiscountProvided = true

In addition, Custom models which conforms Codable protocol can be stored too.

Custom models

struct User: Codable {
    let firstName: String
    let lastName: String
}

struct Defaults {
    @UserDefault("default_user")
    static var defaultUser: User?
}

You can specify any UserDefaults suite for each one. If you want to store on standard suite, no needed to specify it. Default is UserDefaults.standard.

Usage of custom UserDefaults suite

struct Contact: Codable {
    let firstName: String
    let lastName: String
    let phoneNumber: String
}

private let appSuite = UserDefaults(suiteName: "com.strawb3rryx7.userdefault.appsuite")!
private let customSuite = UserDefaults(suiteName: "com.strawb3rryx7.userdefault.customsuite")!

struct Defaults {
    @UserDefault("primary_contact", suite: appSuite)
    static var primaryContact: Contact?
  
    @UserDefault("has_seen_purchase_screen", defaultValue: false, suite: customSuite)
    static var hasSeenPurchaseScreen: Bool
}

userdefault's People

Contributors

batuhansk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mustafakarakus

userdefault's Issues

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.