9

I am trying to pull objects out of my core data store by passing in an array of strings, and pulling only the objects that have a category matching what's in the array.

I have been able to get this code to work, except that it only uses the first item in the array, and won't iterate through the array and match the rest of the items.

This is the code that works for that. I am using the NSPredicate overload that accepts and array.

    func filterTopicCategories() {
        fetchController.topicFetchRequest.predicate = NSPredicate(format: "topicCategory == %@", argumentArray: selectedCategories)
        topicsToSelectFrom = fetchController.fetchTopics()
        }

I've poured through that Apple docs on predicates and all that, and can't seem to quite figure it out. I've spent a few hours searching around google as well. I am not sure if I am just not understanding something correctly, or if I am just doing it completely wrong, I am not sure. Any help would be greatly appreciated.

Thanks

3 Answers 3

14

The parameter argumentArray is for an array of values to replace the placeholders like %@ in the format string.

You are looking for the IN operator:

IN

Equivalent to an SQL IN operation, the left-hand side must appear in the collection specified by the right-hand side. For example, name IN { 'Ben', 'Melissa', 'Nick' }. The collection may be an array, a set, or a dictionary — in the case of a dictionary, its values are used. In Objective-C, you could create a IN predicate as shown in the following example:

NSPredicate *inPredicate = [NSPredicate predicateWithFormat: @"attribute IN %@", aCollection]; 

where aCollection may be an instance of NSArray, NSSet, NSDictionary, or of any of the corresponding mutable classes.

So if topicCategory is a string write

fetchController.topicFetchRequest.predicate = NSPredicate(format: "topicCategory IN %@", selectedCategories)
Sign up to request clarification or add additional context in comments.

1 Comment

yes, I read that and tried that earlier but it still wouldn't work correctly. It was an issue with the argumentArray: label being in there as well.
2

Okay, so I finally stumbled onto this question Swift Core Data Predicate IN Clause that mentioned removing the argumentArray label in the overload. I tried that and then changed my predicateFormat as well. So now it looks like this

    func filterTopicCategories() {
        fetchController.topicFetchRequest.predicate = NSPredicate(format: "ANY topicCategory IN %@", selectedCategories)
        topicsToSelectFrom = fetchController.fetchTopics()

    }

and it seems to work now. Not sure if this is a bug, because the autocomplete in Xcode puts that label there, so, weird.

Anyway, hope this helps someone struggling with the same issue.

Thanks.

Comments

0

this worked for me:

`

    let isWatchLaterPredicate = NSPredicate(format: "isWatchLater == YES")    

let managedContext = appDelegate.managedObjectContext

    let fetchRequestWatchLater = NSFetchRequest<NSManagedObject>(entityName: "WatchList")

    fetchRequestWatchLater.predicate = isWatchLaterPredicate
    print(fetchRequestWatchLater)

    do {

        watchList = try managedContext.fetch(fetchRequestWatchLater)


        print("watch List Items \(isWatchLaterPredicate)")
    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }

        }

`

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.