• This repository has been archived on 26/Aug/2022
  • Stars
    star
    449
  • Rank 97,328 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Why not use UserDefaults to store Codable objects πŸ˜‰

⚠️ Deprecated ⚠️

Please use the new Stores library instead.


Build Status Test Coverage Swift versions Platforms 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!


New in v3.0

  • Both UserDefaultsStore and SingleUserDefaultsStore are thread safe!
  • BREAKING: Use of custom encoder/decoder has been removed.
  • BREAKING: Snapshots have been removed.

Installation

Swift Package Manager

  1. Add the following to your Package.swift file:
dependencies: [
    .package(url: "https://github.com/omaralbeik/UserDefaultsStore.git", from: "3.0.0")
]
  1. Build your project:
$ swift build

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 id property

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

struct User: Codable, Identifiable {
    ...
}
struct Laptop: Codable, Identifiable {
    var id: String { model }
    ...
}

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(id: 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)
let laptop = store.object(withId: "A1278")

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

// Check if store has an object
print(usersStore.hasObject(withId: 10)) // false

// Iterate over all objects in a store
laptopsStore.forEach { laptop in
    print(laptop.name)
}

// 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

Looking to store a single item only?

Use SingleUserDefaultsStore, it enables storing and retrieving a single value of Int, Double, String, or any Codable type.

Requirements

  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
  • Swift 5.0+

Thanks

Special thanks to:

Credits

Icon made by freepik from flaticon.com.

License

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

More Repositories

1

Drops

A Β΅Framework for showing alerts like the one used when copying from pasteboard or connecting Apple pencil
Swift
931
star
2

Stores

Typed key-value storage solution to store Codable types in various persistence layers with few lines of code!
Swift
131
star
3

M3UKit

Modern framework for parsing m3u files
Swift
85
star
4

StackableTableView

A UITableView subclass that enables setting an array of views for both headers and footers utilizing UIStackView
Swift
72
star
5

mnist-coreml

Simple convolutional neural network to predict handwritten digits using Keras + CoreML for WWDC '18 scholarship [Accepted]
Jupyter Notebook
54
star
6

NutritionCal

A food and nutrition app that helps you eat right and make healthier food decisions, get nutrition info for foods and drinks, keep track of what you eat, and sync your eaten foods with Apple's Health App.
Swift
33
star
7

SketchGen

A Swift command line tool for generating source code from sketch files
Swift
26
star
8

omaralbeik.com

React progressive web app backed by Django REST API
20
star
9

flaskbooks

A very light social network & RESTful API for sharing books using flask!
Python
20
star
10

making-mvc-great-again

Use generics, protocols, and extensions to get rid of massive view controllers
Swift
18
star
11

WeekdaysSegmentedControl

A Custom segmented control to select weekdays for iOS
Swift
13
star
12

ForceTouchImageView

Why pinch to zoom when you have 3D TouchΒ πŸ€”
Swift
9
star
13

hacker-news-ios

Hacker News iOS App
Swift
8
star
14

InteractiveNavigationController

[DEPRECATED] Instagram like Custom Interactive UINavigationController with push to left and right
Swift
8
star
15

react-readable

A dynamic social application using the state management features of Redux and React
JavaScript
7
star
16

macOS-setup

The way I set up my mac after a fresh macOS install
7
star
17

VaporTodos-iOS

Simple todos app for iOS using Vapor for the backend
Swift
6
star
18

HNClient

Super easy to use client for Hacker News API
Swift
6
star
19

Spinny1

Simple yet very challenging and addictive game
Swift
5
star
20

swift-composable-architecture-todos

Simple todos app built using Swift Composable Architecture
Swift
5
star
21

MaterialPalette

Use Material Colors Palette in your XCode Project
Swift
5
star
22

BubbleView

iMessage like custom UIView
Swift
5
star
23

website-client

React progressive web app
JavaScript
5
star
24

library-finder

A single page web app, that let you find libraries near a specific place
JavaScript
4
star
25

ios-trivia

Simple trivia iOS app for demo purposes
Swift
3
star
26

VaporTodos-API

Todos API using Vapor3 and Swift4
Swift
3
star
27

UDClient

Udacity Classroom API client for iOS
Swift
2
star
28

dlnd_face_generation

A Generative Adversarial Networks to Generate new images of faces.
HTML
2
star
29

VLCKit

Umbrella repository to import VLCKit using Swift Package Manager.
Swift
2
star
30

VLCPlayer

VLC Player for SwiftUI
Swift
2
star
31

IstanbulNews

for Android Basics Nanodegree submission - an app to display latest news stories from Istanbul using the Guardians API
Java
1
star
32

MarkdownUIKit

Add markdown text to UIKit elements
Swift
1
star
33

nextcoin

Simple Next JS app
TypeScript
1
star
34

dlnd_image_classification

Project 2 for Udacity Deep Learning Nanodegree
HTML
1
star
35

nytimes-gallery

Nothing to see here, just a UICollectionView gallery example :)
Swift
1
star
36

SEN2212

Utilize binary search trees and linked-lists to search efficiently for strings in large text files
Java
1
star
37

SEN2211

SEN2211 Data Structures and Algorithms I Examples
Java
1
star
38

mlnd-titanic-survival-exploration

MLND Titanic Survival Exploration
HTML
1
star
39

dlnd_language_translation

A sequence to sequence model that can translate sentences from English to French.
HTML
1
star
40

py-blog

a multi user blog built by Python and Google App Engine.
Python
1
star
41

VLC

Install VLC using Swift Package Manager
Swift
1
star