1

Let's say I have some data inside Core Data based on the following object:

class TestObject {
   var name: String
   var type: String
}

type can have one of the following names: "red", "green", "blue", "black".

Now I want to filter my data not based on one type, but an array of types, something like this:

public static func typePredicate(types: [String]) -> NSPredicate {

    return NSPredicate(format: "type == %@", types) // this line should test for an array of types, not one type
}

Is this possible to do with NSPredicate ?

2
  • 1
    You're looking for the IN keyword. Ex. ("type IN %@", types); Here's a reference if you would like to read further: Using Predicates with Arrays Commented Dec 28, 2015 at 18:50
  • Thanks. That worked. Commented Dec 28, 2015 at 19:12

1 Answer 1

3

You could try something like below

[NSPredicate predicateWithFormat:@"ANY %K IN %@",object.field,types]

EDIT: In Swift

var types = ["Red","Blue","Green"]
var predicate : NSPredicate = NSPredicate(format: "ANY %K IN %@", 
                               argumentArray: [object.field, types])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.