Code Monkey home page Code Monkey logo

swiftyuserdefaults's Introduction

SwiftyUserDefaults

SwiftyUserDefaults is a set of extensions to make the NSUserDefaults API cleaner, nicer, and at home with Swift's syntax.

Read Swifty APIs: NSUserDefaults for more information about this project.

Fetching data

Defaults["color"].string            // returns String?
Defaults["launchCount"].int         // returns Int?
Defaults["chimeVolume"].double      // returns Double?
Defaults["loggingEnabled"].bool     // returns Bool?
Defaults["lastPaths"].array         // returns NSArray?
Defaults["credentials"].dictionary  // returns NSDictionary?
Defaults["hotkey"].data             // returns NSData?
Defaults["firstLaunchAt"].date      // returns NSDate?
Defaults["anything"].object         // returns NSObject?
Defaults["anything"].number         // returns NSNumber?

SwiftyUserDefaults always returns nil for non-existing values, also for numbers and booleans.

Setting data

Defaults["color"] = "red"
Defaults["launchCount"] = 0

SwiftyUserDefaults infers the right type when setting values.

Optional assignment

Defaults["color"]            // => nil
Defaults["color"] ?= "white" // => "white"
Defaults["color"] ?= "red"   // => "white"

Works like ||= in other languages โ€” sets value only if the left-hand side value is nil.

Arithmetic

Defaults["launchCount"] += 1
Defaults["launchCount"]++

You can use the += and ++ operators to easily work on integer values in the user defaults. If the key didn't exist before operation, the operators assume it was 0.

Existence

if !Defaults.hasKey("hotkey") {
    Defaults.remove("hotkeyOptions")
}

You can use the hasKey method to check for key's existence in the user defaults. remove() is an alias for removeObjectForKey().

Author and license

Radek Pietruszewski

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

Contributing

If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request.

swiftyuserdefaults's People

Contributors

radex avatar jklausa avatar

Watchers

Two Bit Labs avatar James Cloos 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.