2016-09-14 1 views
3

このコード行を追加すると、コンパイル時間が10秒から3分になります。コンパイラを停止せずに複数の配列をマージするには?

var resultsArray = hashTagParticipantCodes + prefixParticipantCodes + asterixParticipantCodes + attPrefixParticipantCodes + attURLParticipantCodes 

このように変更すると、コンパイル時間が通常に戻されます。

var resultsArray = hashTagParticipantCodes 
resultsArray += prefixParticipantCodes 
resultsArray += asterixParticipantCodes 
resultsArray += attPrefixParticipantCodes 
resultsArray += attURLParticipantCodes 

なぜ最初の行は、私のコンパイル時間はそれほど劇的に遅くする原因と私が投稿した5ラインのソリューションよりも、これらの配列をマージするために、よりエレガントな方法はありますでしょうか?

+2

強く関連する:http://stackoverflow.com/questions/29707622/bizarre-swift-compiler-error-expression-too-complex-on-a-string-concatenation –

+0

@MartinRなぜこのようなことが起きたのか、確かに説明していただきありがとうございます。私はこれを言い換えて、ゆっくりとした優雅なソリューションに重点を置いていきます。 – Deco

+1

オペランドはすべて配列ですか?私は一般的なレシピはないと思う。結果に明示的な型の注釈があると、 'var resultsArray:[YourType] = a + b + c + d + e'のようになることがあります。 http://stackoverflow.com/questions/39455123/is-force-unwrapping-a-variable-bad/39455186#comment66231724_39455186と同じ問題についての以下のコメントも参照してください。 –

答えて

8

常に+です。爆発的なコンパイル時に人々が不平を言うたびに、私は "+をチェーンしていますか?"それはいつもはいです。それは、+が非常に過負荷になっているからです。つまり、少なくとも私の簡単な実験では、これはXcode 8の方が劇的に優れていると思います。

あなたは劇的に配列に参加するのではなく、それらを追加することにより、varを必要とせずにこれをスピードアップすることができます。最後に

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

.map{$0}は、あなたがそれを必要とする場合にはそうでない場合は、(バック配列にそれを強制することです怠惰なFlattenCollectionを使うことができます)。

let resultsArray = Array(
        [hashTagParticipantCodes, 
        prefixParticipantCodes, 
        asterixParticipantCodes, 
        attPrefixParticipantCodes, 
        attURLParticipantCodes] 
        .joinWithSeparator([])) 

ただし、Xcode 8を確認してください。私はこれが少なくとも部分的に固定されていると信じています(しかし、.joined()を使うことは、Swift 3でもなおずっと速いです)。

関連する問題