GitXplorerGitXplorer
h

UserDefaultsPlusPlus

public
1 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
4198aaf34e560ea718132c096c5826165a5c0c80

Update Package.swift

hhamzahrmalik committed 4 years ago
Verified
500210e5b4374438ea03c9770c372587d8c785b8

Add SPM support

hhamzahrmalik committed 4 years ago
Verified
9a12555b51f61717f930396be63a22d47b5962b0

Update README.md

hhamzahrmalik committed 5 years ago
Unverified
1f8e96113553e3feab4d104197f66c1d9b1c0984

Initial

hhamzahrmalik committed 5 years ago
Unverified
7da75fdb107419bb4be563dbf42bb2079e29cbeb

Initial commit

hhamzahrmalik committed 5 years ago

README

The README file for this repository.

UserDefaults++

UserDefaults++ is a lightweight wrapper around UserDefaults to make managing keys and their types easier. Simply declare all your keys in one place to be able to save and retrieve items from them in a type safe manner. Plus, you can save any object by providing an encoder and decoder. These are provided automatically for Codable objects

Usage

First, declare your keys. This is best done as static constants

import UserDefaults_

struct Storage {
    //define that we want to store a Bool using the key show_completed
    static let showCompleted = UserDefault<Bool>(key: "show_completed")

    //define that we want to store an array of TodoListItem objects using the key todo_list
    //See 'Storing Complex Objects' to see how TodoListItem is defined
    static let todoList = UserDefault<[TodoListItem]>(key: "todo_list")
}

Then, anywhere in your app, you can refer to these constants to persist or get the value saved to that key

//writing
Storage.showCompleted.persist(true)

//reading
let showCompleted: Bool? = Storage.showCompleted.get()

Storing complex objects

For an object to be storable it must conform to the Storable protocol. Int, Double, Float, Bool, String and URL are already conformed to this protocol for you. Also, arrays of Storable elements are automatically Storable. There is a protocol provided, PlistStorable, that you can conform to which wil allow you to make any struct or class Storable. Conforming to PlistStorable will conform your struct to Codable and will make use of swifts PropertyListEncoder for storage.

Example

//We can make this class Storable for free by making it PlistStorable, as long as all its properties are Codable
struct TodoListItem: PlistStorable {
    
    let name: String
    let added: Date
    var completed: Bool
    
}

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

UserDefaults++ is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'UserDefaults++'

License

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