Reply To: hypothesis for multiple string match

Home Forums OpenEars hypothesis for multiple string match Reply To: hypothesis for multiple string match

#1019798
Halle Winkler
Politepix

OK, this is how you can use fast enumeration (enumeration means looping through the objects in some container) to search for a match against all your keys:

NSArray *keyArray = [objectsInRoom allKeys];

for (NSString *key in keyArray) { 
   if([hypothesis rangeOfString:key].location != NSNotFound) {
        NSLog(@"I found a match in the hypothesis for the key %@ so I'm doing the action for that key and breaking.", key);
      if([key isEqualToString:@"BEER"]) {
         NSLog(@"I found BEER");
         [self doBeerThing];
      } else if([key isEqualToString:@"HAMBURGER"]) {
         NSLog(@"I found HAMBURGER");
         [self doHamburgerThing];
      } else {
         NSLog(@"I don't have an action for the hypothesis that I found.");
      }

      break; // Stop enumerating once we've found something from the keys.
   }
// Next loop will begin if we get here and there are more keys to check
}

There are a few different ways this could be organized for efficiency/clarity. For instance you could skip the first general check for any match and go directly to checking for the specific matches you have in mind, and there are a few different ways you can manage the actual “calling a method having found a specific key” part which I haven’t addressed, but this is a simple and fast approach.