可选绑定,这里的 "Binding" 这个词到底是什么意思?
Optional Binding, what exactly the word "Binding" means here?
可选绑定 是一种查明可选是否包含值的方法,如果包含,则将该值用作临时常量或变量。
var possibleNumber: Int? = 123
if let actualNumber = Int(possibleNumber){
print("\"possibleNumber\" has an integer value of \(actualNumber)")
} else {
print("\"possibleNumber\" could not be converted to an integer")
}
问题Binding是指将有效值赋值给临时constant/variable的动作吗? IE。 "binding" 这两个东西在一起?
Does the Binding mean the action of assigning the valid value into the temporary constant/variable? I.e. "binding" those two things together?
是的。基本上,将值赋给变量名是一种绑定——它 "binds" 名称到值。所以即使这是一个绑定:
let x = 1
if let
的特别之处在于只有当值是一个可以安全解包的 Optional 时才会发生绑定(也就是说,它不是 nil
)。如果它不能安全地解包,它就不会解包并且不会发生绑定(并且 if
条件失败)。
当您将现有 data/code 关联一个新的标识符(名称)来引用它时,您就是 "binding" 它与该标识符。
还有"conditional binding",比如你用if let
的时候。只有当那里有东西时,它才会绑定可选的内容......有条件地。
我们使用可选绑定(使用 if let)来检查可选是否包含该值。如果该值存在,那么我们将该值绑定到仅存在于 if 语句
中的临时局部常量
可选绑定 是一种查明可选是否包含值的方法,如果包含,则将该值用作临时常量或变量。
var possibleNumber: Int? = 123
if let actualNumber = Int(possibleNumber){
print("\"possibleNumber\" has an integer value of \(actualNumber)")
} else {
print("\"possibleNumber\" could not be converted to an integer")
}
问题Binding是指将有效值赋值给临时constant/variable的动作吗? IE。 "binding" 这两个东西在一起?
Does the Binding mean the action of assigning the valid value into the temporary constant/variable? I.e. "binding" those two things together?
是的。基本上,将值赋给变量名是一种绑定——它 "binds" 名称到值。所以即使这是一个绑定:
let x = 1
if let
的特别之处在于只有当值是一个可以安全解包的 Optional 时才会发生绑定(也就是说,它不是 nil
)。如果它不能安全地解包,它就不会解包并且不会发生绑定(并且 if
条件失败)。
当您将现有 data/code 关联一个新的标识符(名称)来引用它时,您就是 "binding" 它与该标识符。
还有"conditional binding",比如你用if let
的时候。只有当那里有东西时,它才会绑定可选的内容......有条件地。
我们使用可选绑定(使用 if let)来检查可选是否包含该值。如果该值存在,那么我们将该值绑定到仅存在于 if 语句
中的临时局部常量