-
Notifications
You must be signed in to change notification settings - Fork 27
NSPredicate
PGLongo edited this page Jan 12, 2015
·
1 revision
- Enum
- Logical operators
- [and] (#and)
- [or] (#or)
- Init
- Class methods
public enum NSLogicOperator : String {
case And = "AND"
case Or = "OR"
}
public enum NSPredicateOperator : String {
case And = "AND"
case Or = "OR"
case In = "IN"
case Equal = "=="
case NotEqual = "!="
case GreaterThan = ">"
case GreaterThanOrEqual = ">="
case LessThan = "<"
case LessThanOrEqual = "<="
}
public func & (left : NSPredicate, right : NSPredicate )-> NSPredicate
public func & (left : NSPredicate, right : [NSPredicate] )-> NSPredicate
public func & (left : [NSPredicate], right : [NSPredicate] )-> NSPredicate
let firstLevelPredicate = NSPredicate(format: "level > 1")!
let secondLevelPredicate = NSPredicate (format: "level =< 36")!
let typePredicate = NSPredicate(format: "type.id = 1")!
let namePredicate = NSPredicate (format: "name == Charmender")!
firstLevelPredicate & secondLevelPredicate
//NSPredicate (format: "level > 1 AND level =< 36")
[firstLevelPredicate, namePredicate] & [secondLevelPredicate]
//NSPredicate (format: "level > 1 AND name == Charmender AND level =< 36")
public func | (left : NSPredicate, right : NSPredicate )-> NSPredicate
public func | (left : NSPredicate, right : [NSPredicate] )-> NSPredicate
public func | (left : [NSPredicate], right : [NSPredicate] )-> NSPredicate
let firstLevelPredicate = NSPredicate(format: "level > 1")!
let secondLevelPredicate = NSPredicate (format: "level =< 36")!
let typePredicate = NSPredicate(format: "type.id = 1")!
let namePredicate = NSPredicate (format: "name == Charmender")!
firstLevelPredicate | secondLevelPredicate
//NSPredicate (format: "level > 1 OR level =< 36")
[firstLevelPredicate, namePredicate] | [secondLevelPredicate]
//NSPredicate (format: "level > 1 OR name == Charmender OR level =< 36")
init firstPredicate : NSPredicate, secondPredicate: NSPredicate, predicateOperator: NSLogicOperator )
let firstLevelPredicate = NSPredicate(format: "level > 1")!
let secondLevelPredicate = NSPredicate (format: "level =< 36")!
let typePredicate = NSPredicate(format: "type.id = 1")!
let namePredicate = NSPredicate (format: "name == Charmender")!
NSPredicate(firstPredicate: firstLevelPredicate, secondPredicate: secondLevelPredicate,
// NSPredicate(format: "(level > 1) AND (level =< 36)")
//Or more complex NSPredicate
NSPredicate(firstPredicate: firstLevelPredicate, secondPredicate: secondLevelPredicate & namePredicate, predicateOperator: .And)
//NSPredicate(format: "(level > 1) AND (level =< 36 AND name == Charmender)")
NSPredicate(firstPredicate: firstLevelPredicate, secondPredicate: secondLevelPredicate & namePredicate, predicateOperator: .Or)
//NSPredicate(format: "(level > 1) OR (level =< 36 AND name == Charmender)")
let firstLevelPredicate = NSPredicate(format: "level > 1")!
let secondLevelPredicate = NSPredicate (format: "level =< 36")!
let typePredicate = NSPredicate(format: "type.id = 1")!
let namePredicate = NSPredicate (format: "name == Charmender")!
var expectedPredicate = NSPredicate(firstPredicate: firstLevelPredicate, secondPredicate: secondLevelPredicate, predicateOperator: .Or)
//NSPredicate(format: "(level > 1) OR (level =< 36)")
expectedPredicate = NSPredicate(firstPredicate: firstLevelPredicate, secondPredicate: secondLevelPredicate & namePredicate, predicateOperator: .Or)
// NSPredicate(format: "(level > 1) OR (level =< 36 AND name == Charmender)")
public class func predicateBuilder(attribute: String!, value: AnyObject, predicateOperator: NSPredicateOperator) -> NSPredicate?
let pokemonsName = ["Charmender", "Charizard"]
let fireType = PokemonFactory.createType(managedObjectContext, id: .Fire, name: .Fire)
NSPredicate.predicateBuilder("name", value: pokemonsName , predicateOperator: .In
// NSPredicate(name: "type IN %@", pokemonsName)
NSPredicate.predicateBuilder("level", value: 1, predicateOperator: .NotEqual)
// NSPredicate(format: "level != 1")
NSPredicate.predicateBuilder("type", value: fireType, predicateOperator: .NotEqual)
// NSPredicate(format: "type != %@", fireType)
NSPredicate.predicateBuilder("level", value: 16, predicateOperator: .GreaterThan)
// NSPredicate(format: "level > 16")
NSPredicate.predicateBuilder("level", value: 16, predicateOperator: .LessThan)
// NSPredicate(format: "level < 16")
NSPredicate.predicateBuilder("level", value: 16, predicateOperator: .GreaterThanOrEqual)
// NSPredicate(format: "level >= 16")
NSPredicate.predicateBuilder("level", value: 16, predicateOperator: .LessThanOrEqual)
// NSPredicate(format: "level <= 16")