Comparing objects in NSSet with objects in NSArray

I pondered this problem for some time, but so far I can’t get a good concise effective solution.

Problem:

I have a list of recipes, which is an NSArray, each recipe object contains ingredients as NSSet objects. The data are related to each other and come from coredata. Now there is another NSArray list that contains the elements (ingredients) that a particular person has.

Now I need to somehow compare the current items that the user has with the ingredients in the recipes, and recommend user recipes in the form of a table with sections, such as all items present, 1 element is missing, two elements are missing and three elements are missing.

How do you guys think that I should approach this problem. I tried a few things, but I end up losing more every time.

Any hints / pointers would be much appreciated

+3
source share
1 answer

NSSet has interesting methods:

  • + (id)setWithArray:(NSArray *)array will allow you to quickly convert your array into a set.

  • - (BOOL)isSubsetOfSet:(NSSet *)otherSet will allow you to find possible recipes.

  • - (BOOL)intersectsSet:(NSSet *)otherSet allows you to find recipes with at least one relevant ingredient.

  • - (NSSet *)objectsPassingTest:(BOOL (^)(id obj, BOOL *stop))predicate can let you find a matching ingredient counter with the correct predicate, something like "is an object in my array?"

+5
source

Source: https://habr.com/ru/post/1772873/


All Articles