可选绑定(bind) 是一种查明可选是否包含值的方法,如果包含,则将该值用作临时常量或变量。
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是指将有效值赋给临时常量/变量的 Action 吗? IE。将这两件事“绑定(bind)”在一起?
最佳答案
Does the Binding mean the action of assigning the valid value into the temporary constant/variable? I.e. "binding" those two things together?
是的。基本上,将值赋给变量名是一种绑定(bind)——它将名称“绑定(bind)”到值。所以即使这是一个绑定(bind):
let x = 1
if let
的特别之处在于只有当值是一个可以安全解包的 Optional 时才会发生绑定(bind)(也就是说,它不是 nil
)。如果它不能安全地解包,它就不会解包并且不会发生绑定(bind)(并且 if
条件失败)。
关于swift - 可选绑定(bind),这里的 "Binding"这个词到底是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37821462/