Created
April 24, 2019 09:48
-
-
Save grdsdev/3c7e8796b42e0f4220e95c3aee6e426b to your computer and use it in GitHub Desktop.
Helper methods for creating NSPredicates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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