Reputation: 296
I'm currently trying to implement some kind of search system in a program of mine, and wanted to use an index, but I'm fairly new at Objective-C. The main idea is to have a 'search' command or text box and when I type a word, it'll show me all the items that include that word. All these 'items' will be listed in a .txt file (hopefully) in alphabetical order. Any help is appreciated.
Upvotes: 0
Views: 463
Reputation: 47729
The ideal thing, if the text file is large and you want to index by the leading characters of each entry, is to create a "dope vector" of sorts, where each entry in the dope vector contains the first few characters of the line, followed by the file offset where the line starts. Note that one dope vector entry can cover a number of file lines, since it's just serving like the index tabs in a dictionary.
But if you want to search for words within a line in your file, you're better off using a SQL database, or some KWIC scheme.
Upvotes: 1
Reputation: 122391
You need to read the .txt file into an NSSet
or some other collection class and you can then search it using something like:
[words filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[c] 'word'"]];
(See the Predicate Guide for details).
Upvotes: 1