Swift App Takes ~6 Minutes To Build

天涯浪子 提交于 2019-12-13 10:58:18

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29360748/swift-app-takes-6-minutes-to-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!