実際的な違いはありません。 (強調鉱山)said by the language guideとして:
バイナリ演算子の左側の式がtry
、try?
、又はtry!
付い ある場合、そのオペレータを 全体のバイナリ表現に適用されます。つまり、括弧を使用して、演算子のアプリケーションの範囲について明示的に とすることができます。ただ、+
よう
// try applies to both function calls
sum = try someThrowingFunction() + anotherThrowingFunction()
// try applies to both function calls
sum = try (someThrowingFunction() + anotherThrowingFunction())
// Error: try applies only to the first function call
sum = (try someThrowingFunction()) + anotherThrowingFunction()
、割り当ても二項演算子です。あなたは
do{
try audioPlayer = AVAudioPlayer(contentsOf: audioURL)
}catch {}
を言うときしたがって、try
はに表現audioPlayer
とAVAudioPlayer(contentsOf: audioURL)
の両方に適用されます。孤独な表現audioPlayer
はおそらくはここでエラーをスローすることはできません - したがって、この場合はtry
のみAVAudioPlayer
のinit(contentsOf:)
、どのできスローへの呼び出しに適用されます。
導出このためof the grammarがある:
// "try" "audioPlayer" "= AVAudioPlayer(contentsOf: audioURL)"
expression → try-operatoropt prefix-expression binary-expressionsopt
prefix-expression → prefix-operatoropt postfix-expression
postfix-expression → primary-expression
primary-expression → identifier generic-argument-clauseopt
identifier → // matches "audioPlayer" (I'm not going to fully derive this bit further)
binary-expressions → binary-expression binary-expressionsopt
binary-expression → assignment-operator try-operatoropt prefix-expression
prefix-expression → prefix-operatoropt postfix-expression
postfix-expression → initializer-expression // matches AVAudioPlayer(contentsOf: audioURL)
あなたはあなたが代入式has the grammar ofという事実を使用している
do{
audioPlayer = try AVAudioPlayer(contentsOf: audioURL)
}catch {}
を言うとき:
binary-expression → assignment-operator try-operatoropt prefix-expression
することができますように参照:try-operator
缶がオペレータの右側に表示されます。prefix-expression
に適用されます。この場合はAVAudioPlayer.init(contentsOf:)
です。
どちらの場合も、AVAudioPlayer.init(contentsOf:)
からスローされる可能性のあるエラーをキャッチしています。最初の例は、オペレータの左側の式からエラーが発生する可能性を示しています。は、おそらくにはありません。
私の個人的な好みと、言語の他の場所でのtry
の配置とより一貫しているオプションは、try
を右側に置くことです。
私は、最初のケースでは割り当てを試み、2番目のケースでは開始を試みることをお勧めします。しかし、私は確信していない、ちょうど推測する。 – JuicyFruit