斯威夫特应用程序需要6〜分钟,以建设 [英] Swift App Takes ~6 Minutes To Build
问题描述
我有大约10万〜字符串数组的斯威夫特应用程序。该阵列看起来是这样的:
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"
]
这需要近6分钟内建立和运行在iPhone模拟器的应用程序。我已经分离出的慢编译时间纳入此数组中的项目。一旦建成,后续启动的速度非常快(直到我必须重新建立)。我能做些什么,以加快构建过程?
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?
推荐答案
根据以上意见,对我来说最好的解决办法是使用一个文本文件中。数据库会工作过,虽然它会在这种情况下,已经添加了不必要的复杂性。该文本文件看起来是这样的:
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
该文件使用的的StreamReader
要点读从<一个href=\"http://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift?answertab=active#tab-top\">this SO帖子。在code,这样做看起来是这样的:
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)
}
}
}
}
生成的 matchingWords
数组只包含必需的元素,关于我的情况600(不〜100K元素!)。现在,应用程序编译没有延迟。从文件中读取数据,并追加到 matchingWords
阵列比赛大约需要5秒钟,这是我的需要可以接受的,但可以如果需要进一步优化。
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.
这篇关于斯威夫特应用程序需要6〜分钟,以建设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!