

GitHub - omaralbeik/UserDefaultsStore: Why not use UserDefaults to store Codable...
source link: https://github.com/omaralbeik/UserDefaultsStore
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
UserDefaultsStore
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
usesInt
for its id, whileLaptop
usesString
. 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.
Recommend
-
75
readme.md Defaults
-
131
README.md Flight School Guide to Swift Codable Sample Code This repository contains sample code used in the Flight School Guide to Swift Codable....
-
46
README.md Zephyr ?️ Effortlessly sync UserDefaults over iCloud
-
42
One of my students asked in Slack the other day about how to use UserDefaults properly in a Clean Swift architecture. I’ve seen multiple takes on this question in other blogs. Here’s my take: ...
-
72
我们经常会使用Keychain和UserDefaults来存储iOS App数据。Keychain是iOS设备中的一个安全的存储容器,可以用来为应用保存敏感信息比如用户名,密码,网络密码,认证令牌。
-
56
With the UserDefaults class, you can save settings and properties related to application or user data. Any data saved to the defaults system will persist through application restarts. In this tutorial we will save the sta...
-
37
README.md SecureDefaults for iOS, macOS
-
7
When to use UserDefaults, Keychain, or Core Data There are many ways to store data locally in iOS app. UserDefaults, Keychain and Core Data are some of the most popular ways to persist...
-
13
An effective way to clear entire Userdefaults in Swift May 19, 2020 • Rizwan •...
-
4
26 Mar 2021 software-dev
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK