Per the comments above, the best solution for me was to use a text file. A database would have worked too, though it would've added unnecessarily complexity in this case. The text file looks something like this:
a
as
...
zebra
The file is read using the StreamReader
gist from this SO post. The code for doing so looks like this:
if let aStreamReader = StreamReader(path: "/path/to/file") {
for word in aStreamReader {
// This is where I'm testing the following conditions:
// 1) Does the word have a specific number of characters (e.g. 4 or 7)?
// 2) Do all the characters in the word exist in a stored string?
// e.g "lifeline", where "file" would match, but "lifelines" wouldn't.
// This code is only here for context.
if contains(characterCountsToMatch, countElements(word)) {
if stringToMatch.containsCharsInString(word) {
matchingWords.append(word)
}
}
}
}
The resulting matchingWords
array contains only the necessary elements–about 600 in my case (not ~100k elements!). The application now compiles with no delay. Reading from the file and appending matches to the matchingWords
array takes about 5 seconds, which is acceptable for my needs, but could be further optimized if needed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…