Your question isn't clear, but I am assuming that each element in your databaseArray
is an array of NSStrings.
The following method pulls each line in each element in databaseArray
apart into separate words and verifies that at least one word from each line in the supplies list.
- (NSArray *) intersectCandidates:(NSArray *)candidates withSupplies:(NSArray *)supplies
{
NSMutableArray *output=[[NSMutableArray alloc]init];
NSSet *suppliesSet=[NSSet setWithArray:supplies];
for (NSArray *candidateLines in candidates) {
int count=0;
for (NSString *candidateLine in candidateLines) {
NSArray *wordArray=[candidateLine componentsSeparatedByString:@" "];
for (NSString *word in wordArray) {
if ([suppliesSet contains:word]) {
++count;
break;
}
}
}
if (count == [candidateLines count]) { // We matched one word from each line
[output addObject:candidateLines];
}
}
return [NSArray arrayWithArray:output]; // Return a non-mutable copy
}
Because of the aforementioned XY problem, I am not sure that this is the most efficient way of solving your actual problem.