迅速的应用程序需要~6分钟才能build立
我有一个约100kstring数组的Swift应用程序。 数组看起来像这样:
let strings: [String] = [ "a", "as", // 99,998 elements later... "zebra" ]
在iOS模拟器中构build和运行应用程序需要将近6分钟的时间。 我已经将这个数组包含在项目中的时间缩短了。 一旦build成,后续发射速度非常快(直到我不得不再次build立)。 我能做些什么来加快构build过程?
根据上面的评论,对我来说最好的解决scheme是使用文本文件。 一个数据库也可以工作,尽pipe在这种情况下会增加不必要的复杂性。 文本文件看起来像这样:
a as ... zebra
该文件是从这个SOpost使用StreamReader
要点读取的 。 这样做的代码如下所示:
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 (eg 4 or 7)? // 2) Do all the characters in the word exist in a stored string? // eg "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秒,这对我的需求是可以接受的,但是如果需要的话可以进一步优化。