Skip to content
PGLongo edited this page Jan 12, 2015 · 1 revision

Contents

NSLogicOperator

public enum NSLogicOperator : String {
    case And = "AND"
    case Or = "OR"
}

NSPredicateOperator

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 = "<="
}

And

public func & (left : NSPredicate, right : NSPredicate )-> NSPredicate
public func & (left : NSPredicate, right : [NSPredicate] )-> NSPredicate
public func & (left : [NSPredicate], right : [NSPredicate] )-> NSPredicate

Example

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")

Or

public func | (left : NSPredicate, right : NSPredicate )-> NSPredicate
public func | (left : NSPredicate, right : [NSPredicate] )-> NSPredicate
public func | (left : [NSPredicate], right : [NSPredicate] )-> NSPredicate

Example

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

init firstPredicate : NSPredicate, secondPredicate: NSPredicate, predicateOperator: NSLogicOperator )

Example

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)")

Example

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)")

PredicateBuilder

public class func predicateBuilder(attribute: String!, value: AnyObject, predicateOperator: NSPredicateOperator) -> NSPredicate? 

Example

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")