Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit e4c57ac

Browse files
authored
v1.2.2 (#28)
1 parent fb30184 commit e4c57ac

File tree

8 files changed

+44
-19
lines changed

8 files changed

+44
-19
lines changed

.swiftlint.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
disabled_rules:
22
- identifier_name
33
- type_name
4-
5-
excluded:
6-
- Example

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a>
1212
<a href="https://swift.org/package-manager/"><img src="https://img.shields.io/badge/SPM-compatible-4BC51D.svg?style=flat" alt="Swift Package Manager compatible" /></a>
1313
<a href="https://swift.org"><img src="https://img.shields.io/badge/Swift-4.2-orange.svg" alt="Swift" /></a>
14-
<a href="https://developer.apple.com/xcode"><img src="https://img.shields.io/badge/Xcode-9.4-blue.svg" alt="Xcode"></a>
14+
<a href="https://developer.apple.com/xcode"><img src="https://img.shields.io/badge/Xcode-10-blue.svg" alt="Xcode"></a>
1515
<a href="https://github.com/omaralbeik/UserDefaultsStore/blob/master/LICENSE"><img src="https://img.shields.io/badge/License-MIT-red.svg" alt="MIT"></a>
1616
</p>
1717

@@ -61,6 +61,7 @@ dependencies: [
6161
## Usage
6262

6363
Let's say you have 2 structs; `User` and `Laptop` defined as bellow:
64+
6465
```swift
6566
struct User: Codable {
6667
var id: Int
@@ -81,6 +82,7 @@ Here is how you store them in **UserDefaultsStore**:
8182

8283

8384
### 1. Conform to the `Identifiable` protocol and set the `idKey` property
85+
8486
The `Identifiable` protocol lets UserDefaultsStore knows what is the unique id for each object.
8587

8688
```swift
@@ -97,15 +99,18 @@ struct Laptop: Codable, Identifiable {
9799
}
98100
```
99101

100-
* Notice how `User` uses `Int` for its id, while `Laptop` uses `String`. Swift rocks 🤘
102+
> Notice how `User` uses `Int` for its id, while `Laptop` uses `String`, in fact the id can be any `Hashable` type. UserDefaults uses Swift keypaths to refer to properties without actually invoking them. Swift rocks 🤘
103+
101104

102105
### 2. Create UserDefaults Stores
106+
103107
```swift
104108
let usersStore = UserDefaultsStore<User>(uniqueIdentifier: "users")!
105109
let laptopsStore = UserDefaultsStore<Laptop>(uniqueIdentifier: "laptops")!
106110
```
107111

108112
### 3. Voilà, you're all set!
113+
109114
```swift
110115
let macbook = Laptop(model: "A1278", name: "MacBook Pro")
111116
let john = User(userId: 1, firstName: "John", lastName: "Appleseed", laptop: macbook)
@@ -118,6 +123,7 @@ try! usersStore.save([jane, steve, jessica])
118123

119124
// Get an object from store
120125
let user = store.object(withId: 1)
126+
let laptop = store.object(withId: "A1278")
121127

122128
// Get all objects in a store
123129
let laptops = laptopsStore.allObjects()
@@ -143,22 +149,27 @@ let usersCount = usersStore.objectsCount
143149

144150

145151
## Looking to store a single item only?
146-
Use [`SingleUserDefaultsStore`](https://github.com/omaralbeik/UserDefaultsStore/blob/master/Sources/SingleUserDefaultsStore.swift), it enables storing and retrieving a single `Codable` object only.
152+
153+
Use [`SingleUserDefaultsStore`](https://github.com/omaralbeik/UserDefaultsStore/blob/master/Sources/SingleUserDefaultsStore.swift), it enables storing and retrieving a single value of `Int`, `Double`, `String`, or any `Codable` type.
147154

148155

149156
## Requirements
157+
150158
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
151-
- Xcode 9+
152-
- Swift 4+
159+
- Xcode 10.0+
160+
- Swift 4.2+
153161

154162

155163
## Thanks
164+
156165
Special thanks to [Paul Hudson](https://twitter.com/twostraws) for his [article](https://www.hackingwithswift.com/articles/57/how-swift-keypaths-let-us-write-more-natural-code) on how to use Swift keypaths to write more natural code.
157166

158167

159168
## Credits
169+
160170
Icon made by [freepik](https://www.flaticon.com/authors/freepik) from [flaticon.com](https://www.flaticon.com).
161171

162172

163173
## License
174+
164175
UserDefaultsStore is released under the MIT license. See [LICENSE](https://github.com/omaralbeik/UserDefaultsStore/blob/master/LICENSE) for more information.

Sources/Identifiable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import Foundation
2525

26-
/// Conform to `Identifiable` protocol in uniquely identified objects you want to store in a UserDefaultsStore.
26+
/// Conform to `Identifiable` protocol in uniquely identified objects you want to store in a `UserDefaultsStore`.
2727
public protocol Identifiable {
2828

2929
/// ID type.

Sources/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.2.1</string>
18+
<string>1.2.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

Sources/SingleUserDefaultsStore.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,25 @@
2323

2424
import Foundation
2525

26-
/// SingleUserDefaults Store.
26+
/// `SingleUserDefaultsStore` offers a convenient way to store a single `Codable` object in `UserDefaults`.
2727
open class SingleUserDefaultsStore<T: Codable> {
2828

2929
/// Store's unique identifier.
30+
///
3031
/// **Warning**: Never use the same identifier for two -or more- different stores.
3132
public let uniqueIdentifier: String
3233

33-
/// JSON encoder. _(default is JSONEncoder())_
34+
/// JSON encoder. _default is `JSONEncoder()`_
3435
open var encoder = JSONEncoder()
3536

36-
/// JSON decoder. _(default is JSONDecoder())_
37+
/// JSON decoder. _default is `JSONDecoder()`_
3738
open var decoder = JSONDecoder()
3839

3940
/// UserDefaults store.
4041
private var store: UserDefaults
4142

4243
/// Initialize store with given identifier.
44+
///
4345
/// **Warning**: Never use the same identifier for two -or more- different stores.
4446
///
4547
/// - Parameter uniqueIdentifier: store's unique identifier.
@@ -75,15 +77,23 @@ open class SingleUserDefaultsStore<T: Codable> {
7577
// MARK: - Helpers
7678
private extension SingleUserDefaultsStore {
7779

80+
/// Enclose the object in a dictionary to enable single object storing.
81+
///
82+
/// - Parameter object: object.
83+
/// - Returns: dictionary enclosing object.
7884
func generateDict(for object: T) -> [String: T] {
7985
return ["object": object]
8086
}
8187

88+
/// Extract object from dictionary.
89+
///
90+
/// - Parameter dict: dictionary.
91+
/// - Returns: object.
8292
func extractObject(from dict: [String: T]) -> T? {
8393
return dict["object"]
8494
}
8595

86-
/// store key for object.
96+
/// Store key for object.
8797
var key: String {
8898
return "\(uniqueIdentifier)-single-object"
8999
}

Sources/UserDefaultsStore.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,25 @@
2323

2424
import Foundation
2525

26-
/// UserDefaults Store.
26+
/// `UserDefaultsStore` offers a convenient way to store a collection of `Codable` objects in `UserDefaults`.
2727
open class UserDefaultsStore<T: Codable & Identifiable> {
2828

2929
/// Store's unique identifier.
30+
///
3031
/// **Warning**: Never use the same identifier for two -or more- different stores.
3132
public let uniqueIdentifier: String
3233

33-
/// JSON encoder. _(default is JSONEncoder())_
34+
/// JSON encoder. _default is `JSONEncoder()`_
3435
open var encoder = JSONEncoder()
3536

36-
/// JSON decoder. _(default is JSONDecoder())_
37+
/// JSON decoder. _default is `JSONDecoder()`_
3738
open var decoder = JSONDecoder()
3839

3940
/// UserDefaults store.
4041
private var store: UserDefaults
4142

4243
/// Initialize store with given identifier.
44+
///
4345
/// **Warning**: Never use the same identifier for two -or more- different stores.
4446
///
4547
/// - Parameter uniqueIdentifier: store's unique identifier.

UserDefaultsStore.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "UserDefaultsStore"
3-
s.version = "1.2.1"
3+
s.version = "1.2.2"
44
s.summary = "Why not use UserDefaults to store Codable objects 😉"
55
s.description = <<-DESC
66
You love Swift"s Codable protocol and use it everywhere, here is an easy and very light way to store - reasonable amount 😅 - of Codable objects, in a couple lines of code.
@@ -15,7 +15,7 @@ Pod::Spec.new do |s|
1515
s.module_name = "UserDefaultsStore"
1616
s.source = { :git => "https://github.com/omaralbeik/UserDefaultsStore.git", :tag => s.version }
1717
s.source_files = "Sources/**/*.swift"
18-
s.swift_version = "4.1"
18+
s.swift_version = "4.2"
1919
s.requires_arc = true
2020

2121
s.ios.deployment_target = "8.0"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict/>
5+
</plist>

0 commit comments

Comments
 (0)