0

I used the TableSearch sample from Apple to implement my own search in the app I'm working on.

It seems that the sample is bugged. The scope buttons will display the result controller only if something is entered in the search field. But, looking at the code, that doesn't seem the case because they specifically check for search items being empty or not:

if !searchItemsPredicate.isEmpty {
    /** We have a scope type and other fields to search on -
        so match up the fields of the Product object AND its product type.
    */
    let compPredicate1 = NSCompoundPredicate(orPredicateWithSubpredicates: searchItemsPredicate)
    let compPredicate2 = NSPredicate(format: "(SELF.type == %ld)", selectedScopeButtonIndex)

    finalCompoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [compPredicate1, compPredicate2])
} else {
    // Match up by product scope type only.
    finalCompoundPredicate = NSCompoundPredicate(format: "(SELF.type == %ld)", selectedScopeButtonIndex)
}

So, if the search items predicate is empty (meaning nothing has been entered in the search field), the code is supposed to filter according to the product type. But that doesn't work when you run the app. However, if you just put a space into the search field, then the code filters the products correctly and displays the result controller. I've been trying to fix this but I haven't been able to find the relation between the content of the search field and the scope buttons.

Anybody has any idea?

  • What's the question? – matt May 01 '22 at 22:41
  • How can I make it works? How can I truly make the scope buttons filter the results without having to enter something in the search field? – Nemesys Software May 03 '22 at 13:01
  • If there is nothing in the search field there is nothing to filter. The scope buttons do not filter; they determine what "filter" means. They do not _do_ the search, they define _what_ is to be searched. The bug is not in the example, it's in your (mis)understanding of what scope means. – matt May 03 '22 at 13:06
  • The sample can be downloaded from https://docs-assets.developer.apple.com/published/703396f957/DisplayingSearchableContentByUsingASearchController.zip – Nemesys Software May 03 '22 at 13:24
  • Matt, if you check the excerpt of code I posted above, you'll see that the code checks if there is something or not in the search field and, if there isn't, the code tries to filter the products based on their type. Check the comment "Match up by product scope type only". – Nemesys Software May 04 '22 at 14:20

0 Answers0