Reputation: 188
I have two model like below:
class Topic : NSObject {
var subTopics : [SubTopic]!
var topicId : Int!
var topicName : String!
class SubTopic : NSObject {
var subTopicDesc : String!
var subTopicDescText : String!
var subTopicId : Int!
var subTopicName : String!
}
and array of Topic defined in my viewcontroller :
var rightsDataArray: [Topic] = []
And here is my Search logic :
let pred = NSPredicate(format: "topicName CONTAINS %@ OR SUBQUERY(subTopics, $SubTopics, $SubTopics.subTopicDescText CONTAINS %@).@count > 0 OR SUBQUERY(subTopics, $SubTopics, $SubTopics.subTopicName CONTAINS %@).@count > 0",searchBar.text!,searchBar.text!,searchBar.text!)
let filteredArray = (self.rightsDataArray as NSArray).filtered(using: pred)
And here is the result if search string is Health:
If the searchedText is present in subTopic[0], then I want to remove subTopic[1],subTopic[2],subTopic[3] from resulting subTopics Array, and only retrieve subTopic[0].
Upvotes: 0
Views: 454
Reputation: 8563
You are filtering the top level Topic
array, and nothing in you code will touch the subTopics subarray. The predicate is deciding whether or not to add a particular Topic
(the top level object) to the filtered array based on its subTopics. But once a Topic matches the criteria (ie having at least one subTopic that has the search terms) the Topic
is added.
It seems like you want to modify the Topic
's subTopics
array to contains less objects. For that you have to iterate through every Topic
and modify it's subTopics
property.
Upvotes: 1