Coder Social home page Coder Social logo

userdefaultsstore's Introduction

UserDefaultsStore

Build Status Platforms Cocoapods codecov Swift Xcode MIT

tl;dr

You love Swift's Codable protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve -reasonable amount 😅- of Codable objects, in a couple lines of code!

Installation

CocoaPods

To integrate UserDefaultsStore into your iOS project using CocoaPods, specify it in your Podfile:

pod 'UserDefaultsStore'

Carthage

To integrate UserDefaultsStore using Carthage, add the following to your Cartfile:

github "omaralbeik/UserDefaultsStore"

Manually

Add the Sources folder to your Xcode project.

Usage

Let's say you have 2 structs; User and Laptop defined as bellow:

struct User: Codable {
    var id: Int
    var firstName: String
    var lastName: String
    var laptop: Laptop?
}
struct Laptop: Codable {
    var model: String
    var name: String
}

Here is how you store them in UserDefaultsStore:

1. Conform to the Identifiable protocol and set the idKey property

The Identifiable protocol lets UserDefaultsStore knows what is the unique id for each object.

struct User: Codable, Identifiable {
    static let idKey = \User.id
    ...
}
struct Laptop: Codable, Identifiable {
    static let idKey = \Laptop.model
    ...
}
  • Notice how User uses Int for its id, while Laptop uses String. Swift rocks 🤘

2. Create UserDefaults Stores

let usersStore = UserDefaultsStore<User>(uniqueIdentifier: "users")!
let laptopsStore = UserDefaultsStore<Laptop>(uniqueIdentifier: "laptops")!

3. Voilà, you're all set!

let macbook = Laptop(model: "A1278", name: "MacBook Pro")
let john = User(userId: 1, firstName: "John", lastName: "Appleseed", laptop: macbook)

// Save an object to a store
try! usersStore.save(john)

// Save an array of objects to a store
try! usersStore.save([jane, steve, jessica])

// Get an object from store
let user = store.object(withId: 1)

// Get all objects in a store
let laptops = laptopsStore.allObjects()

// Delete an object from a store
usersStore.delete(withId: 1)

// Delete all objects in a store
laptops.deleteAll()

// Know how many objects are stored in a store
let usersCount = usersStore.objectsCount

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 9+
  • Swift 4+

Thanks

Special thanks to Paul Hudson for his article on how to use Swift keypaths.

License

UserDefaultsStore is released under the MIT license. See LICENSE for more information.

userdefaultsstore's People

Contributors

omaralbeik avatar

Watchers

James Cloos avatar Duc Nguyen 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.