A Turkish version of this document can be found here.
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 (Recommended)
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" ~> 1.0
Swift Package Manager
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: "1.4.2")
]
)
Next, add UserDefaultsStore
to your targets dependencies like so:
.target(
name: "YOUR_TARGET_NAME",
dependencies: [
"UserDefaultsStore",
]
),
Then run swift package update
.
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:
Identifiable
protocol and set the idKey
property
1. Conform to the 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
, in fact the id can be anyHashable
type. UserDefaults uses Swift keypaths to refer to properties without actually invoking them. 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)
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.
class
instead of struct
Note about using At the moment, only final
classes are supported, please take this into consideration before using the library.
Requirements
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 10.0+
- Swift 4.2+
Thanks
Special thanks to:
- Paul Hudson for his article on how to use Swift keypaths to write more natural code.
- Batuhan Saka for translating this document into Turkish.
Credits
Icon made by freepik from flaticon.com.
License
UserDefaultsStore is released under the MIT license. See LICENSE for more information.