All Projects → omaralbeik → Userdefaultsstore

omaralbeik / Userdefaultsstore

Licence: mit
Why not use UserDefaults to store Codable objects 😉

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Userdefaultsstore

swift-standard-clients
Client declarations and live implementations for standard iOS managers
Stars: ✭ 28 (-93.27%)
Mutual labels:  tvos, watchos, userdefaults
Persistencekit
Store and retrieve Codable objects to various persistence layers, in a couple lines of code!
Stars: ✭ 121 (-70.91%)
Mutual labels:  userdefaults, tvos, watchos
Sqift
Powerful Swift wrapper for SQLite
Stars: ✭ 119 (-71.39%)
Mutual labels:  database, tvos, watchos
PotentCodables
🧪 PotentCodables - A potent set of implementations and extensions to the Swift Codable system
Stars: ✭ 32 (-92.31%)
Mutual labels:  tvos, watchos, codable
Nshipster.com
A journal of the overlooked bits in Objective-C, Swift, and Cocoa. Updated weekly.
Stars: ✭ 280 (-32.69%)
Mutual labels:  tvos, watchos
X
Easier cross platform Mac & iOS development with Swift
Stars: ✭ 270 (-35.1%)
Mutual labels:  tvos, watchos
Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (-29.33%)
Mutual labels:  tvos, watchos
Json
Micro framework for easily parsing JSON in Swift with rich error messages in less than 100 lines of code
Stars: ✭ 395 (-5.05%)
Mutual labels:  tvos, watchos
Datez
📆 Breeze through Date, DateComponents, and TimeInterval with Swift!
Stars: ✭ 254 (-38.94%)
Mutual labels:  tvos, watchos
Web3.swift
A pure swift Ethereum Web3 library
Stars: ✭ 295 (-29.09%)
Mutual labels:  tvos, watchos
Crypto
Swift CommonCrypto wrapper
Stars: ✭ 328 (-21.15%)
Mutual labels:  tvos, watchos
Swiftui Charts
🚀 SwiftUI Charts with custom styles
Stars: ✭ 272 (-34.62%)
Mutual labels:  tvos, watchos
Wikipediakit
Wikipedia API Client Framework for Swift on macOS, iOS, watchOS, and tvOS
Stars: ✭ 270 (-35.1%)
Mutual labels:  tvos, watchos
Skeletonui
☠️ Elegant skeleton loading animation in SwiftUI and Combine
Stars: ✭ 275 (-33.89%)
Mutual labels:  tvos, watchos
Xestimonitors
An extensible monitoring framework written in Swift
Stars: ✭ 269 (-35.34%)
Mutual labels:  tvos, watchos
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (-27.4%)
Mutual labels:  tvos, watchos
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+791.83%)
Mutual labels:  tvos, watchos
Valet
Valet lets you securely store data in the iOS, tvOS, or macOS Keychain without knowing a thing about how the Keychain works. It’s easy. We promise.
Stars: ✭ 3,712 (+792.31%)
Mutual labels:  tvos, watchos
Sentry Cocoa
The official Sentry SDK for iOS, tvOS, macOS, watchOS
Stars: ✭ 370 (-11.06%)
Mutual labels:  tvos, watchos
Waterwheel.swift
The Waterwheel Swift SDK provides classes to natively connect iOS, macOS, tvOS, and watchOS applications to Drupal 7 and 8.
Stars: ✭ 415 (-0.24%)
Mutual labels:  tvos, watchos

Build Status Test Coverage Platforms Cocoapods Carthage compatible Swift Package Manager compatible 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!


Introducing v2.0

  • Removed the Identifiable protocol in favor of Swift's Identifiable.
  • Increased deployment targets to iOS 13.0, tvOS 13.0, macOS 10.15, and watchOS 6.0.
  • Objects defined as non-final classes can now be used as well.
  • Added new generateSnapshot() and restoreSnapshot(_:) methods to generate and restore a Snapshot object that can be saved (e.g. to iCloud) and restored later.
  • Fixed a bug where objectsCount might run out of sync with the actual count of objects in store.

Installation

Swift Package Manager (Recommended)

You can use The Swift Package Manager to install UserDefaultsStore by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .package(url: "https://github.com/omaralbeik/UserDefaultsStore.git", from: "2.0.0")
    ]
)

Next, add UserDefaultsStore to your targets dependencies like so:

.target(
    name: "YOUR_TARGET_NAME",
    dependencies: [
        "UserDefaultsStore",
    ]
),

Then run swift package update.

CocoaPods

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

pod 'UserDefaultsStore'
Carthage

To integrate UserDefaultsStore into your Xcode project using Carthage, specify it in your Cartfile:

github "omaralbeik/UserDefaultsStore" ~> 2.0.0
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

// Create a snapshot
let snapshot = usersStore.generateSnapshot()

// Restore a pre-generated snapshot
try? usersStore.restoreSnapshot(snapshot)

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].