ObjC / iPhone dictionary grep?
I have a large number of strings in my iPhone app containing random combinations of letters, and I'd like to check each of these random strings against a dictionary. I'm looking for a way to grep each string with /usr/share/dict/words and return the result (or number of results) back to my app. Is this possible in an iPhone app?
Edit: Thanks for all the suggestions. Originally I wanted to execute a grep and ideally pipe the results back into my app, but after some more digging and playing around with NSTask it seems its not possible on an iPhone. I ended up loading all the words into a big NSDictionary with this approach:
NSString* allWords = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSASCIIStringEncoding error:NULL]; NSArray* wordArray = [allWords componentsSeparatedByString开发者_JS百科:@"\n"];
Easiest way would be to add the words to a sqlite database and do grep
against them using an sql query or NSPredicate
I think you could handle this with standard code, unless you are using regular expressions. In your question the term "grep" seems to just mean substring search from a list of words.
Assuming that you have access to the dictionary of words (like maybe it's included in the app bundle) try this:
NSString *dictString = [NSString stringWithContentsOfFile:pathToDictionary encoding:NSUTF8StringEncoding error:NULL];
NSArray *words = [dictString componentsSeparatedByString:@"\n"]; // maybe @"\r\n" ?
for (NSString *word in words)
{
// do your comparison here
NSRange wordRange = [myBigString rangeOfString:word];
if (wordRange.location!=NSNotFound)
{
NSLog(@"It's peanut butter jelly time! [%@]", word);
}
}
精彩评论