Skip to content
This repository has been archived by the owner. It is now read-only.
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
iOS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

framework

UserDefaults Carthage Swift License

Platforms

Build Status

iOS macOS watchOS tvOS

Usage

Define Static Keys for easy access to data stored in UserDefaults

extension DefaultsKeys {
  static let key = DefaultsKey<String>("key")
  static let number = DefaultsKey<Double>("number")
  static let condition = DefaultsKey<Bool>("condition")
  // eg. struct SomeCodable : Codeable { ... }
  static let codeable = DefaultsKey<SomeCodable>("somecodeable")
}

Write to UserDefaults

Static keys

UserDefaults.standard[.key] = "Value"
UserDefaults.standard[.number] = -3.4
UserDefaults.standard[.condition] = true
UserDefaults.standard[.codeable] = SomeCodable()

String Keys

UserDefaults.standard["key"] = "Value"
UserDefaults.standard["number"] = -3.4
UserDefaults.standard["condition"] = true

Read from UserDefaults

Static Keys

var string = UserDefaults.standard[.key]
// string = Optional<String>

if UserDefaults.standard[.condition] {
  // true
}

let someCodable: SomeCodable? = UserDefaults.standard[.codeable]
// someCodable = Optional<SomeCodable>

String Keys

let object = UserDefaults.standard["key"]
// object = Optional<Any>

let string = UserDefaults.standard.string("key")
// string = Optional<String>

Alter UserDefaults

Static Keys

UserDefaults.standard[.key] += ". Other value."

UserDefaults.standard[.number] *= 1.2

String Keys

UserDefaults.standard["key"] = UserDefaults.standard.string(forKey: "key") ?? "" + ". Other value."

UserDefaults.standard["number"] = UserDefaults.standard.double("number") * 1.2

Installation

UserDefaults is available through Carthage. To install just write into your Cartfile:

github "divadretlaw/UserDefaults"

License

See LICENSE

Copyright Β© 2018 David Walter (www.davidwalter.at)