“try"的位置差异关键词 [英] Difference in placement of "try" keyword
问题描述
我有一个音频播放器的全局变量.在变量初始化之前放置try word有什么区别
I have a global variable of audio player. What's the difference between placing try word before the variable initialization
do{
try audioPlayer = AVAudioPlayer(contentsOf: audioURL)
}catch {}
并在调用构造函数之前放置 try
and placing try before calling the constructor
do{
audioPlayer = try AVAudioPlayer(contentsOf: audioURL)
}catch {}
当然上面两种情况没有任何编译错误.谢谢
Of course two cases above haven't any compilation errors. Thanks
推荐答案
没有实际区别.作为 由语言指南说(强调我的):
There is no practical difference. As said by the language guide (emphasis mine):
当二元运算符左侧的表达式为用 try
、try?
或 try!
标记,该运算符适用于整个二进制表达式.也就是说,您可以使用括号来表示明确运营商的适用范围.
When the expression on the left hand side of a binary operator is marked with
try
,try?
, ortry!
, that operator applies to the whole binary expression. That said, you can use parentheses to be explicit about the scope of the operator’s application.
// 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()
就像 +
一样,赋值也是一个二元运算符.因此,当你说
Just like +
, assignment is also a binary operator. Therefore, when you say
do{
try audioPlayer = AVAudioPlayer(contentsOf: audioURL)
}catch {}
try
适用于 两个 表达式 audioPlayer
和 AVAudioPlayer(contentsOf: audioURL)
.单独的表达式 audioPlayer
不可能在此处抛出错误 - 因此在这种情况下 try
仅适用于对 AVAudioPlayer的调用code> 的
init(contentsOf:)
,可以抛出.
The try
applies to both expressions audioPlayer
and AVAudioPlayer(contentsOf: audioURL)
. The lone expression audioPlayer
cannot possibly throw an error here – therefore the try
in this case only applies to the call to AVAudioPlayer
's init(contentsOf:)
, which can throw.
推导 语法 是:
// "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 {}
您正在使用赋值表达式 有的语法:
You're using the fact that the assignment expression has the grammar of:
binary-expression → assignment-operator try-operatoropt prefix-expression
如您所见,try-operator
可以也出现在此处的运算符右侧,因此将应用于 前缀-表达式
——在本例中是AVAudioPlayer.init(contentsOf:)
.
As you can see, the try-operator
can also appear on the right hand side of the operator here, and therefore will apply to the prefix-expression
– which in this case is AVAudioPlayer.init(contentsOf:)
.
因此,在这两种情况下,您都会捕获从 AVAudioPlayer.init(contentsOf:)
可能抛出的错误.第一个示例仅包含从运算符左侧的表达式抛出错误的可能性,而它不可能做到这一点.
So in both cases, you're catching the error potentially thrown from AVAudioPlayer.init(contentsOf:)
. The first example just includes the possibility of an error being thrown from the expression on the left hand side of the operator, which it cannot possibly do.
使用你觉得更舒服的任何一个——我个人的偏好,与在语言的其他地方放置 try
更一致的选项是把 try
在右侧.
Use whichever you feel more comfortable with – my personal preference, and the option which is more consistent with the placement of try
in other places in the language, is to put the try
on the right-hand side.
这篇关于“try"的位置差异关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!