3

I've been pondering over this problem for a while now but I am not able to get a nice concise efficient solution yet.

Problem:

I have a recipe list which is an NSArray, every recipe object contains ingredients as NSSet objects. Data is one to many relationship & is coming from coredata. Now there is another list an NSArray which contains items(ingredients) a particular person currently have.

Now I have to somehow compare currently present items which user have with ingredients in recipes & recommend user recipes in a table view with sections like all items present, 1 item missing, two items missing and three items missing.

How do you guys think I should approach this problem. I have tried a few things but I end up getting even more lost each time.

Any help/pointers will be highly appreciated

1 Answer 1

5

You have interesting methods in NSSet:

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

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

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

  • - (NSSet *)objectsPassingTest:(BOOL (^)(id obj, BOOL *stop))predicate can allow you to find matching ingredients count, with the proper predicate, which is something like 'is object in my array?'

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.