Skip to content

Instantly share code, notes, and snippets.

@grdsdev
Created April 24, 2019 09:48
Show Gist options
  • Save grdsdev/3c7e8796b42e0f4220e95c3aee6e426b to your computer and use it in GitHub Desktop.
Save grdsdev/3c7e8796b42e0f4220e95c3aee6e426b to your computer and use it in GitHub Desktop.
Helper methods for creating NSPredicates
import Foundation
extension NSPredicate {
static func equal(_ field: String, value: Any, caseInsensitive: Bool = false) -> NSPredicate {
let caseInsensitiveModifier = caseInsensitive ? "[c]" : ""
return NSPredicate(format: "%K ==\(caseInsensitiveModifier) %@", argumentArray: [field, value])
}
static func notEqual(_ field: String, value: Any, caseInsensitive: Bool = false) -> NSPredicate {
let caseInsensitiveModifier = caseInsensitive ? "[c]" : ""
return NSPredicate(format: "%K !=\(caseInsensitiveModifier) %@", argumentArray: [field, value])
}
static func any(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "ANY %K = %@", argumentArray: [field, value])
}
static func lessThan(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K < %@", argumentArray: [field, value])
}
static func lessThanOrEqual(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K <= %@", argumentArray: [field, value])
}
static func greaterThan(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K > %@", argumentArray: [field, value])
}
static func greaterThanOrEqual(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K >= %@", argumentArray: [field, value])
}
static func beginsWith(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K BEGINSWITH[c] %@", argumentArray: [field, value])
}
static func endsWith(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K ENDSWITH[c] %@", argumentArray: [field, value])
}
static func contains(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K CONTAINS[c] %@", argumentArray: [field, value])
}
static func like(_ field: String, value: Any) -> NSPredicate {
return NSPredicate(format: "%K LIKE[c] %@", argumentArray: [field, value])
}
static func between(_ field: String, this: Any, and that: Any) -> NSPredicate {
return NSPredicate(format: "%K BETWEEN {%@, %@}", argumentArray: [field, this, that])
}
static func `in`(_ field: String, values: [Any], caseInsensitive: Bool = false) -> NSPredicate {
let caseInsensitiveModifier = caseInsensitive ? "[c]" : ""
return NSPredicate(format: "%K IN\(caseInsensitiveModifier) %@", argumentArray: [field, values])
}
static func notIn(_ field: String, values: [Any], caseInsensitive: Bool = false) -> NSPredicate {
let caseInsensitiveModifier = caseInsensitive ? "[c]" : ""
return NSPredicate(format: "NOT (%K IN\(caseInsensitiveModifier) %@)", argumentArray: [field, values])
}
// MARK: - Compound Predicates
static func and(_ predicates: NSPredicate...) -> NSPredicate {
return NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
}
static func or(_ predicates: NSPredicate...) -> NSPredicate {
return NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
}
static func not(_ predicate: NSPredicate) -> NSPredicate {
return NSCompoundPredicate(notPredicateWithSubpredicate: predicate)
}
}
// MARK: - Usage
query(where: .equal("name", value: "Guilherme"))
query(where: .notEqual("name", value: "Guilherme"))
query(where: .beginsWith("name", value: "Gui"))
query(where: .and(.lessThan("age", value: 18),
.beginsWith("name", value: "Gui")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment