PredicatePal is a Swift DSL for building NSPredicate
objects.
NSPredicate
is a Foundation class that specifies how data should be fetched or filtered. The class is an integral part of many other system frameworks such as CloudKit, CoreData, Contacts, EventKit and HealthKit. Its query language is expressive and natural, but since it's typically built by a runtime parser, it can be hard to figure out and slow to build.
PredicatePal replaces this query language with a compile-time DSL. Using the magic of Swift generics and operator overloading, this framework:
- Checks syntax at compile time. No more crashes with query syntax errors, get Xcode autocompletion and Quick Help on your side!
- Prevents type errors. Can't combine expressions of incompatible types.
- Plugs into the type system. Mix and match
NSPredicate
terms with Swift constants and expressions. - Runs fast. 2x the speed of the runtime parser.
- Has unit tests.
As an independent project:
- In the Terminal, run
git clone https://github.com/pixelglow/PredicatePal.git
. - Within the PredicatePal directory, open the PredicatePal.xcodeproj Xcode project.
- In the Xcode project, select either the PredicatePal (iOS), PredicatePal (OS X) or PredicatePal (tvOS) scheme from the drop down.
- You can now build, test (OS X only) or analyze with the selected scheme.
- The built frameworks and test cases are in a subdirectory of ~/Library/Developer/Xcode/DerivedData.
As a project integrated with your own workspace:
- In the Terminal, run
cd workspace
thengit submodule add https://github.com/pixelglow/PredicatePal.git
. - In your Xcode workspace, choose the File > Add Files to "workspace" menu item, then within the PredicatePal subdirectory pick the PredicatePal.xcodeproj Xcode project.
- Build the PredicatePal project at least once.
- In any project target that will use PredicatePal:
- In General > Embedded Binaries, click on + and add the PredicatePal.framework corresponding to your target OS. The framework should appear in both Embedded Binaries and Linked Frameworks and Libraries.
- You can now build, test or analyze those project targets.
Using Carthage:
- In your
Cartfile
, specifygithub "pixelglow/PredicatePal"
. - Run
carthage update
to build.
Import:
import PredicatePal
Build an NSPredicate
:
let age: Key<Int>("age")
let predicate = *(age < 21 || age > 42) // NSPredicate(format:"age < 21 OR age > 42")
Getting started: Tutorial
Full syntax guide: Syntax
- Build: Xcode 7 and later.
- Link: Only system libraries; no third-party libraries needed.
- CoreLocation.framework
- Foundation.framework
- Run: Mac OS X 10.10 (Yosemite) or iOS 8.0 and later.
- Follow us on Twitter: @pixelglow.
- Raise an issue on PredicatePal issues.
PredicatePal is licensed with the BSD license.
- Tip with ChangeTip.