The most basic implementation involves overriding this method ... Definitely not optimal, but you should get the idea:
- (NSArray *) completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index { // this would be defined somewhere else, but just for example.. NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"]; NSMutableArray *matchedNames = [NSMutableArray array]; NSString *toMatch = [[self string] substringWithRange:charRange]; for(NSString *username in usernames) { [matchedNames addObject:username]; } return matchedNames; // that it. }
As soon as you start to have a lot of data, you will need to use strategies for preliminary searches, storing words in hashes with partial text fragments (for example, "Hello" will be placed in 4 different arrays in the NSDictionary keys for "H", "He", " Hel "," Hell ". Repeat all the words in your dictionary.
If you want to support automatic completion, just call the "complete:" method when you find that the text is changing in your control.
source share