简体   繁体   中英

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:@"\\n"];

最简单的方法是将单词添加到sqlite数据库,并使用sql查询或NSPredicate对它们进行grep

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);
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM