我正在开发一个基于单词的游戏,我需要将我的180,000个单词字典(1.9MB)加载到一个数组中,以便求解算法可以使用它。字典只是每行一个单词,如下所示:
a
ab
abs
absolutely
etc
...现在,我使用以下代码将该文件加载到数组中:
NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"txt"];
NSString *stringFromFile = [[NSString alloc]
initWithContentsOfFile:txtPath
encoding:NSUTF8StringEncoding
error:&error ];
for (NSString *word in [stringFromFile componentsSeparatedByString:@"\r\n"]) {
[wordsArray addObject:word];
} 这在iPhone 4上大约需要3-4秒,在老的iOS设备上可能会更慢。有没有更快的方法来做这件事?
发布于 2012-04-14 02:45:37
将单词列表预先加载到SQLite数据库中,然后使用SQL查询使用您拥有的任何模式进行搜索,可能会更快。您可以利用索引来使它更快。
https://stackoverflow.com/questions/10146171
复制相似问题