如何在不降低编译器速度的情况下合并多个数组?

Dec*_*nna 5 arrays compilation ios swift

添加这行代码会导致编译时间从10秒增加到3分钟.

var resultsArray = hashTagParticipantCodes + prefixParticipantCodes + asterixParticipantCodes + attPrefixParticipantCodes + attURLParticipantCodes
Run Code Online (Sandbox Code Playgroud)

将其更改为此会使编译时间恢复正常.

var resultsArray = hashTagParticipantCodes
resultsArray += prefixParticipantCodes
resultsArray += asterixParticipantCodes
resultsArray += attPrefixParticipantCodes
resultsArray += attURLParticipantCodes
Run Code Online (Sandbox Code Playgroud)

为什么第一行导致我的编译时间如此急剧减慢并且有一种更优雅的方式来合并这些数组而不是我发布的5行解决方案?

Rob*_*ier 11

它始终是+.每次人们抱怨爆炸性的编译时间,我都会问"你有链接+吗?" 它始终是肯定的.这是因为+如此严重超载.也就是说,我认为这在Xcode 8中要好得多,至少在我的快速实验中是这样.

你可以大大提高速度,而不需要var加入数组而不是添加它们:

let resultsArray = [hashTagParticipantCodes,
                    prefixParticipantCodes,
                    asterixParticipantCodes, 
                    attPrefixParticipantCodes,
                    attURLParticipantCodes]
                   .joinWithSeparator([]).map{$0}
Run Code Online (Sandbox Code Playgroud)

.map{$0}在到底是迫使它回到一个阵列(如果你需要,否则,你可以只使用懒惰FlattenCollection).你也可以这样做:

let resultsArray = Array(
                   [hashTagParticipantCodes,
                    prefixParticipantCodes,
                    asterixParticipantCodes, 
                    attPrefixParticipantCodes,
                    attURLParticipantCodes]
                   .joinWithSeparator([]))
Run Code Online (Sandbox Code Playgroud)

但检查Xcode 8; 我相信这至少是部分修复的(但.joined()即使在Swift 3中,使用仍然要快得多).