comparing objects in NSSet with objects in NSArray
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.
N开发者_开发技巧ow 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
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?'
精彩评论