Giter Site home page Giter Site logo

victorknust / swiftyuserdefaults Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sunshinejr/swiftyuserdefaults

0.0 1.0 0.0 284 KB

Swifty API for NSUserDefaults

Home Page: http://radex.io/swift/nsuserdefaults/

License: MIT License

Swift 94.07% Ruby 5.93%

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().

Installation

The simplest way to install this library is to copy Src/SwiftyUserDefaults.swift to your project. There's no step two!

Note: Using Xcode 6.3 beta? You'll find a Swift 1.2-compatible version on the swift-1.2 branch

CocoaPods

You can also install this library using CocoaPods. Just add this line to your Podfile:

pod 'SwiftyUserDefaults'

Then import library module like so:

import SwiftyUserDefaults

Note that this requires CocoaPods 0.36 as well as iOS 8 or OS X 10.9+

Contributing

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

Author and license

Radek Pietruszewski

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

swiftyuserdefaults's People

Contributors

radex avatar maximkhatskevich avatar jklausa avatar narfdotpl avatar

Watchers

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.