Skip to content

Commit 0183b30

Browse files
update README
1 parent 8b85ee9 commit 0183b30

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

README.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ This should open a Finder window with the important files needed for PrediKit lo
114114
The downside to this is that you can not update PrediKit easily. You would need to repeat these steps each time you wanna grab the latest and greatest! 😱
115115

116116
#Usage
117+
118+
***PSA: IF YOU HATE STRINGLY TYPED APIs LIKE I DO, THEN CHECK OUT THE SECTION ON SWIFT 3's #keyPath() AT THE BOTTOM OF THE README!!!***
119+
117120
PrediKit tries to make `NSPredicate` creation easy. **Heavily** inspired by [SnapKit's](https://github.com/SnapKit/SnapKit) API, the API for PrediKit is extremely similar for people who love it as much as I do. Check it out. This example creates a predicate used to fetch a `ManagedObject` from `CoreData`:
118121

119122
```swift
@@ -211,32 +214,32 @@ let predicate = NSPredicate(ManagedLegend.self) { includeIf in
211214
}
212215
```
213216

214-
#Selector Extension Pattern
215-
Personally, I love using a variation of the [Selector Extension Pattern](https://medium.com/swift-programming/swift-selector-syntax-sugar-81c8a8b10df3#.bypt7blba) when using PrediKit. It allows you to avoid misspelling your property names when using the API. It also allows you to rename your selector properties at will. By renaming, every instance of that selector used by PrediKit should give you a compiler error so you don't miss a beat and can feel safe knowing you haven't missed any property names in a name change refactor. By creating a Selector extension like so:
217+
# #keyPath() + PrediKit = 💖
218+
PrediKit becomes MUCH more expressive and safer when using Swift 3's #keyPath syntax. I don't know about you but I HATE stringly typed APIs. The best part of #keyPath is that you get autocompletion of your properties and a way to get sub properties without using PrediKit's `.member` functions:
216219

217220
```swift
218-
import Foundation
221+
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
222+
includeIf.string(#keyPath(ManagedLegend.title)).equals("The Almighty Kraken")
223+
}
224+
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
225+
includeIf.string(#keyPath(ManagedLegend.bestFriend.title)).equals("The Cool Elf")
226+
}
227+
```
219228

220-
extension Selector {
221-
private enum Names: String {
222-
case title
223-
case birthdate
224-
case age
225-
case friends
226-
case isAwesome
227-
case isHungry
228-
}
229229

230-
private init(_ name: Names) {
231-
self.init(name.rawValue)
232-
}
230+
# Selector Extension Pattern Variation
231+
Personally, I love using a variation of the [Selector Extension Pattern](https://medium.com/swift-programming/swift-selector-syntax-sugar-81c8a8b10df3#.bypt7blba) when using PrediKit. It allows you to avoid misspelling your property names when using the API. It also allows you to rename your keypath properties at will. By renaming, every instance of that keyPath used by PrediKit should give you a compiler error so you don't miss a beat and can feel safe knowing you haven't missed any property names in a name change refactor. By creating a String extension like so:
233232

234-
static let title = Selector(.title)
235-
static let birthdate = Selector(.birthdate)
236-
static let age = Selector(.age)
237-
static let friends = Selector(.friends)
238-
static let isAwesome = Selector(.isAwesome)
239-
static let isHungry = Selector(.isHungry)
233+
```swift
234+
import Foundation
235+
236+
extension String {
237+
static let title = #keyPath(Kraken.title)
238+
static let birthdate = #keyPath(Kraken.birthdate)
239+
static let age = #keyPath(Kraken.age)
240+
static let friends = #keyPath(Kraken.friends)
241+
static let isAwesome = #keyPath(Kraken.isAwesome)
242+
static let isHungry = #keyPath(Kraken.isHungry)
240243
}
241244
```
242245

@@ -245,7 +248,7 @@ PrediKit becomes a lot more expressive now:
245248
```swift
246249
//BEFORE
247250
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
248-
includeIf.string("title").equals("The Almighty Kraken")
251+
includeIf.string(#keyPath(ManagedLegend.title)).equals("The Almighty Kraken")
249252
}
250253
//AFTER
251254
let predicate = NSPredicate(ManagedLegend.self) { includeIf in

0 commit comments

Comments
 (0)