실제적인 차이는 없습니다.
이진 연산자의 좌측 식은 가
try
,
try?
또는
try!
표시되어
그 오퍼레이터 가 전체 이진 표현에 적용 said by the language guide (강조 광산)한다. 즉, 괄호를 사용하여 연산자 응용 프로그램의 범위에 대해 명시 적으로 이 될 수 있습니다. 그냥 +
같은
// 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)
당신은 당신이 사실을 사용하고
do{
audioPlayer = try AVAudioPlayer(contentsOf: audioURL)
}catch {}
을 말할 때 그 대입 표현식 has the grammar of :
binary-expression → assignment-operator try-operatoropt prefix-expression
당신이 할 수 참조, try-operator
수 이 여기에있는 연산자의 오른쪽에 나타나므로 prefix-expression
에 적용됩니다 (이 경우 AVAudioPlayer.init(contentsOf:)
).
두 경우 모두 AVAudioPlayer.init(contentsOf:)
에서 발생할 수있는 오류를 catch하고 있습니다. 첫 번째 예는 연산자의 왼쪽에있는 표현식에서 오류가 발생할 가능성을 포함합니다. 은 가능하면이 될 수 없습니다.
내 개인적인 취향과 언어의 다른 곳에서 try
의 위치와 더 일치하는 옵션은 try
을 오른쪽에 붙이면됩니다.내보기에서
나는 첫 번째 경우가 할당을 시도하고 두 번째 시도가 시작되도록 제안합니다. 그러나 나는 잘 모르겠다. – JuicyFruit