All Projects → andreadelfante → Predicateflow

andreadelfante / Predicateflow

Licence: mit
Write amazing, strong-typed and easy-to-read NSPredicate.

Programming Languages

swift
15916 projects
flow
126 projects

Projects that are alternatives of or similar to Predicateflow

Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (+200%)
Mutual labels:  tvos, watchos, cocoapods
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+3685.71%)
Mutual labels:  tvos, watchos, cocoapods
Web3.swift
A pure swift Ethereum Web3 library
Stars: ✭ 295 (+201.02%)
Mutual labels:  tvos, watchos, cocoapods
Mirrordiffkit
Graduation from messy XCTAssertEqual messages.
Stars: ✭ 168 (+71.43%)
Mutual labels:  library, tvos, watchos
Swiftyattributes
A Swifty API for attributed strings
Stars: ✭ 1,303 (+1229.59%)
Mutual labels:  tvos, watchos, cocoapods
Datez
📆 Breeze through Date, DateComponents, and TimeInterval with Swift!
Stars: ✭ 254 (+159.18%)
Mutual labels:  tvos, watchos, cocoapods
Swiftlinkpreview
It makes a preview from an URL, grabbing all the information such as title, relevant texts and images.
Stars: ✭ 1,216 (+1140.82%)
Mutual labels:  tvos, watchos, cocoapods
Cocoalumberjack
A fast & simple, yet powerful & flexible logging framework for Mac and iOS
Stars: ✭ 12,584 (+12740.82%)
Mutual labels:  tvos, watchos, cocoapods
Xcake
🍰 Describe Xcode projects in a human readable format and (re)generate one on demand.
Stars: ✭ 549 (+460.2%)
Mutual labels:  tvos, watchos, cocoapods
Swiftframeworktemplate
A template for new Swift iOS / macOS / tvOS / watchOS Framework project ready with travis-ci, cocoapods, Carthage, SwiftPM and a Readme file
Stars: ✭ 527 (+437.76%)
Mutual labels:  tvos, watchos, cocoapods
Fire
🔥A delightful HTTP/HTTPS networking framework for iOS/macOS/watchOS/tvOS platforms written in Swift.
Stars: ✭ 243 (+147.96%)
Mutual labels:  tvos, watchos, cocoapods
Open Source Ios Apps
📱 Collaborative List of Open-Source iOS Apps
Stars: ✭ 28,826 (+29314.29%)
Mutual labels:  tvos, watchos, cocoapods
Iso8601
ISO8601 date parser and writer
Stars: ✭ 213 (+117.35%)
Mutual labels:  tvos, watchos, cocoapods
Skeletonui
☠️ Elegant skeleton loading animation in SwiftUI and Combine
Stars: ✭ 275 (+180.61%)
Mutual labels:  tvos, watchos, cocoapods
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (+80.61%)
Mutual labels:  tvos, watchos, cocoapods
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (+208.16%)
Mutual labels:  tvos, watchos, cocoapods
Cdmarkdownkit
An extensive Swift framework providing simple and customizable markdown parsing.
Stars: ✭ 158 (+61.22%)
Mutual labels:  tvos, watchos, cocoapods
Version
Represent and compare versions via semantic versioning (SemVer) in Swift
Stars: ✭ 160 (+63.27%)
Mutual labels:  tvos, watchos, cocoapods
Swiftuipager
Native Pager in SwiftUI
Stars: ✭ 430 (+338.78%)
Mutual labels:  tvos, watchos, cocoapods
Guitar
A Cross-Platform String and Regular Expression Library written in Swift.
Stars: ✭ 641 (+554.08%)
Mutual labels:  tvos, watchos, cocoapods

PredicateFlow

Build Status CocoaPods Compatible Platform contributions welcome License

Write amazing, strong-typed and easy-to-read NSPredicate. This library allows you to write flowable NSPredicate, without guessing attribution names, predicate operation or writing wrong arguments type.

Supported platforms

  • iOS 9.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+

Installation

CocoaPods is actually the only way to install it.

Cocoapods

CocoaPods 0.39.0+ is required to build this library

  1. Add pod 'PredicateFlow' to your Podfile and run pod install

  2. In Xcode, click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the plus icon in the top left

  3. Drag the new Run Script phase above the Compile Sources phase and below Check Pods Manifest.lock, expand it and paste the following script:

    "$PODS_ROOT/Sourcery/bin/sourcery" --sources "$PODS_ROOT/PredicateFlow/PredicateFlow/Classes/Utils/" --sources "$SRCROOT" --templates "$PODS_ROOT/PredicateFlow/PredicateFlow/Templates/PredicateFlow.stencil" --output "$SRCROOT/PredicateFlow.generated.swift"
    

    In case you are using PredicateFlow-Realm, past the following script instead of the above one:

    "$PODS_ROOT/Sourcery/bin/sourcery" --sources "$PODS_ROOT/PredicateFlow/PredicateFlow/Classes/Utils/" --sources "$SRCROOT"  --sources "$PODS_ROOT/RealmSwift" --templates "$PODS_ROOT/PredicateFlow/PredicateFlow/Templates/PredicateFlow-Realm.stencil" --output "$SRCROOT/PredicateFlow.generated.swift"
    

    For Xcode 10 only, add a new Output Files and paste the following:

    $SRCROOT/PredicateFlow.generated.swift
    
  4. Build your project. In Finder you will now see a PredicateFlow.generated.swift in the $SRCROOT-folder, drag the PredicateFlow.generated.swift files into your project and uncheck Copy items if needed

Tip: Add the *.generated.swift pattern to your .gitignore file to prevent unnecessary conflicts.

Usage

Define a class/struct with its own attributes, which implements PredicateSchema:

import PredicateFlow

class Dog: PredicateSchema {
	
	private var name: String = ""
	private var age: Int = 0
	private var isHungry: Bool = false
	private var owner: Person?
}

class Person: PredicateSchema {
	
	enum Sex {
		case male
		case female
	}
	
	private var name: String = ""
	private var birth: Date?
	private var sex: Sex!
	private var dogs: [Dog] = []
}

Build the project. PredicateFlow will autogenerate attributes references to build predicates, putting them in structures.

// Generated using Sourcery 0.10.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT


import PredicateFlow

/// The "Dog" Predicate Schema
internal struct DogSchema: GeneratedPredicateSchema {
    private var compoundFieldBuilder: CompoundFieldBuilder

    /// DO NOT USE THIS INIT DIRECTLY!
    internal init(compoundFieldBuilder: CompoundFieldBuilder) {
        self.compoundFieldBuilder = compoundFieldBuilder
    }

    /// "name" property
    internal var name: StringPredicateProperty { return builder.string("name") }
    /// "name" property for static access
    internal static var name: StringPredicateProperty { return DogSchema().name }
    
    // Other properties of Dog class autogenerated...
}
/// The "Person" Predicate Schema
internal struct PersonSchema: GeneratedPredicateSchema {
    private var compoundFieldBuilder: CompoundFieldBuilder

    /// DO NOT USE THIS INIT DIRECTLY!
    internal init(compoundFieldBuilder: CompoundFieldBuilder) {
        self.compoundFieldBuilder = compoundFieldBuilder
    }

    /// "name" property
    internal var name: StringPredicateProperty { return builder.string("name") }
    /// "name" property for static access
    internal static var name: StringPredicateProperty { return PersonSchema().name }
    
    // Other properties of Person class autogenerated...
}

To type a floawable NSPredicate, just write:

DogSchema.age.isEqual(10).query()
// or
// Vanilla mode: 
// NSPredicate("age == %@", 10)

You can also write compound predicate, and use deeper fields:

PredicateBuilder(DogSchema.age > 10)
    .and(DogSchema.isHungry.isTrue)
    .and(DogSchema.age.between(1, 10))
    .and(DogSchema.owner.element().name == "Foo")
    .or(DogSchema.owner.element().dogs.maxElements().age > 10)
    .or(DogSchema.owner.element().dogs.anyElements().name == "Foo")
    .build()
    
// Vanilla mode: 
// NSPredicate("age > %@ AND isHungry == %@ AND age BETWEEN %@ AND owner.name == %@ OR [email protected] > %@ OR ANY owner.dogs.name == %@", 10, true, [1, 10], "Foo", 10, "Foo")

PredicateFlow can also build KeyPaths, and you can use it to get a strong-typed one.

DogSchema.age.keyPath()
DogSchema.owner.element().dogs.keyPath()

// Vanilla mode:
// "age"
// "owner.dogs"

PredicateFlow/Realm

If you want to use flowable and strong-typed queries in Realm, add both pod 'PredicateFlow' and pod 'PredicateFlow/Realm' to your Podfile and run pod install.

let realm = try! Realm()
realm.objects(Dog.self)
    .filter(DogSchema.age.isGreater(than: 10))
    .filter(DogSchema.isHungry.isTrue)
    .sorted(DogSchema.age.ascending())
    
// Vanilla mode:
realm.objects(Dog.self)
    .filter("age > %@", 10)
    .filter("isHungry == %@", true)
    .sorted("age", ascending: true)

Contributing

PredicateFlow is an open source project, so feel free to contribute. You can open an issue for problems or suggestions, and you can propose your own fixes by opening a pull request with the changes.

License

PredicateFlow is available under the MIT license. See the LICENSE file for more info.

Attributions

This library is powered by Sourcery.

Author

Andrea Del Fante, [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].