CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ | 
| LangLanguage | SwiftSwift | 
| License | MIT | 
| ReleasedLast Release | Sep 2016 | 
| SPMSupports SPM | ✓ | 
Maintained by Hector Matos.
A Swift NSPredicate DSL for iOS & OS X inspired by SnapKit, lovingly written in Swift, and created by that weird dude at KrakenDev.
If you’re familiar with the intuitive feel of the SnapKit API, then you’ll feel right at home with PrediKit! 
Documentation is generated by Jazzy and can be found here for your convenience!
Because I wanted to! Also, because NSPredicate creation is hard. When working with CoreData you use NSPredicates to fetch persisted objects. CoreData is hard enough, so why have the additional complexity of using a complex string-based predicate system?
The language you need to use to create predicate formats are completely string-based which can lead to a host of issues:
SQL-like language that comes with the creation of predicate formats. In fact, an entire cheatsheet by the awesome guys at Realm was created because of this!NSPredicate system, then nothing happens. It just fails silently. At least, I think it does. I’m currently writing this on 2 hours of sleep. Don’t quote me on that.Well, hopefully it fixes all of the above and more. Currently, it:
matches or doesNot.match another value.builder closures do not need capture semantics as each closure is a @noescape closure. Read here if you don’t know what that means 🤓.PrediKit can be included in your project through any of these methods:
If you prefer not to use either of the aforementioned dependency managers, you can integrate PrediKit into your project manually.
First, copy and paste these commands into Terminal:
git clone https://github.com/KrakenDev/PrediKit.git
open PrediKit/Sources/This should open a Finder window with the important files needed for PrediKit located in the Sources folder of the repo. Drag these folders into your project (preferable in a folder named “PrediKit”) and code away! Since you would be copying these files into your project directly, there is no need for the import PrediKit line in any of the files that you need it.
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! 
PrediKit tries to make NSPredicate creation easy. Heavily inspired by SnapKit’s 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:
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    includeIf.string("title").equals("The Almighty Kraken")
}To check if a property is nil:
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    includeIf.string("title").equalsNil()
}PrediKit can also query member properties. Say you have a class structure like this:
class Captain: NSObject {
    var name: String
}
class Ship: NSObject {
    var captain: Captain
}And you want to create these predicates:
let someCaptain = Captain()
NSPredicate(format: "captain == %@ && captain.name == 'Chief Supreme'", someCaptain)Creating the above with PrediKit is easy and expressive:
let someCaptain = Captain()
let predicate = NSPredicate(Ship.self) { includeIf
    let includeIfShipCaptain = includeIf.member("captain", ofType: Captain.self)
    includeIfShipCaptain.equals(someCaptain) &&
    includeIfShipCaptain.string("name").equals("Chief Supreme")
}PrediKit also overloads the &&, ||, and ! operators. This allows you compound and specify whether or not to include your includers (Crappy name, I know. Feel free to give me suggestions).
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    //Include any ManagedLegend instance if the property named "string" is NOT nil and does NOT equal "The Almighty Kraken"
    !includeIf.string("title").equalsNil() &&
    !includeIf.string("title").equals("The Almighty Kraken") &&
    //Also include any ManagedLegend instance if the date property named "birthdate" is in the past or if the bool property "isAwesome" is true.
    includeIf.date("birthdate").isEarlierThan(NSDate()) ||
    includeIf.bool("isAwesome").isTrue()
}You can even create includers conditionally!
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    let isKrakenQuery = includeIf.string("title").equals("The Almighty Kraken")
    let birthdateQuery = includeIf.date("birthdate").isEarlierThan(NSDate())
    let isAwesomeQuery = includeIf.bool("isAwesome").isTrue
    if shouldCheckBirthdate {
        (isKrakenQuery && birthdateQuery) || isAwesomeQuery
    } else {
        isKrakenQuery || isAwesomeQuery
    }
}I don’t know about y'all, but the SQL-like IN operator was hard to wrap my head around. PrediKit makes this a little more human-readable:
let awesomePeeps = ["Kraken", "Cthulhu", "Voldemort", "Ember", "Umber", "Voldemort"]
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    includeIf.string("title").matchesAnyValueIn(awesomePeeps)
}PrediKit also has built-in support for subquery predicates:
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    includeIf.string("title").equals("The Almighty Kraken") &&
    //Only include Krakens that have more than three hungry cerberus friends
    includeIf.collection("cerberusFriends").subquery(ManagedCerberus.self) {
        $0.bool("isHungry").isTrue()
        return .IncludeIfMatched(.Amount(.IsGreaterThan(3)))
    }
}Personally, I love using a variation of the Selector Extension Pattern 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:
import Foundation
extension Selector {
    private enum Names: String {
        case title
        case birthdate
        case age
        case friends
        case isAwesome
        case isHungry
    }
    private init(_ name: Names) {
        self.init(name.rawValue)
    }
    static let title = Selector(.title)
    static let birthdate = Selector(.birthdate)
    static let age = Selector(.age)
    static let friends = Selector(.friends)
    static let isAwesome = Selector(.isAwesome)
    static let isHungry = Selector(.isHungry)
}PrediKit becomes a lot more expressive now:
//BEFORE
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    includeIf.string("title").equals("The Almighty Kraken")
}
//AFTER
let predicate = NSPredicate(ManagedLegend.self) { includeIf in
    includeIf.string(.title).equals("The Almighty Kraken")
}PrediKit is licensed under the MIT license. Check out the LICENSE file to learn more.