Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

ios - Swift App Takes ~6 Minutes To Build

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...