Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a Swift app with an array of about ~100k strings. The array looks something like this:

let strings: [String] = [
    "a",
    "as",
    // 99,998 elements later...
    "zebra"
]

It takes nearly 6 minutes to build and run the app in the iOS Simulator. I've isolated the slow build time to the inclusion of this array in the project. Once built, subsequent launches are very fast (until I have to build again). What can I do to speed up the build process?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
293 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...