Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more complex NSPredicate and predicate builder #8

Merged
merged 8 commits into from
Dec 17, 2014

Conversation

PGLongo
Copy link
Collaborator

@PGLongo PGLongo commented Dec 14, 2014

Added predicateBuilder that create a NSPredicate from 3 parameters:

  1. Attribute name
  2. Attribute value (any object)
  3. NSPredicateOperator
enum NSPredicateOperator : String {
    case And = "AND"
    case Or = "OR"
    case In = "IN"
    case Equal = "=="
    case NotEqual = "!="
    case GreaterThan = ">"
    case GreaterThanOrEqual = ">="
    case LessThan = "<"
    case LessThanOrEqual = "<="
}

    class func predicateBuilder(attribute: String!, value: AnyObject, predicateOperator: NSPredicateOperator ) -> NSPredicate? {
        var predicate = NSPredicate(format: "%K \(predicateOperator.rawValue) $value", attribute)
        predicate = predicate?.predicateWithSubstitutionVariables(["value" : value]);
        return predicate
    }

For example:

NSPredicate.predicateBuilder("level", value: 16, predicateOperator: .LessThan)
NSPredicate(format: "level < 16")
...
NSPredicate.predicateBuilder("name", value: "Charmender", predicateOperator: .Equal)
NSPredicate(format: "name == \"Charmender\"")

Using predicateBuilder I have improved findFirstOrCreateWithAttribute, now it works with all value and not only with strings

class func findFirstOrCreateWithAttribute(attribute: String!, value: AnyObject!, context: NSManagedObjectContext = SuperCoreDataStack.defaultStack.managedObjectContext!, handler: ((NSError!) -> Void)! = nil) -> NSManagedObject {
        var predicate = NSPredicate.predicateBuilder(attribute, value: value, predicateOperator: .Equal)
        return findFirstOrCreateWithPredicate(predicate, context: context, handler)
}

I have also added a new init method to allow the creation of more complex NSPredicate (the kind of firstCondition AND ( secondCondition OR thirdCondition)

 convenience init?(firstPredicate : NSPredicate, secondPredicate: NSPredicate, predicateOperator: NSLogicOperator ) {
            self.init(format: "(\(firstPredicate)) \(predicateOperator.rawValue) (\(secondPredicate))")
}

@michaelarmstrong
Copy link
Owner

Fantastic, can you merge develop into this branch so its mergable. Thanks

@PGLongo
Copy link
Collaborator Author

PGLongo commented Dec 17, 2014

Fixed!

michaelarmstrong added a commit that referenced this pull request Dec 17, 2014
Added more complex NSPredicate and predicate builder
@michaelarmstrong michaelarmstrong merged commit 423b298 into michaelarmstrong:master Dec 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants