为什么Swift的可选绑定在某些情况下会以'nil'成功? [英] Why does Swift's optional binding succeed with 'nil' in certain cases?

查看:81
本文介绍了为什么Swift的可选绑定在某些情况下会以'nil'成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple的 Swift语言文档可选绑定(又名,如果让)将检查里面的值可选和提取将该值转换为变量或常量。但这与我所看到的不符。例如

Apple's Swift language documentation says that optional binding (a.k.a. if let) will "check for a value inside an optional" and "extract that value into" a variable or constant). But this doesn't match what I'm seeing. For example

var x: Int? = nil

if let y1: Int? = x {
    println("y1 = \(y1)") // This is printed, suggesting that x is not checked "inside", but left as Optional(nil) (!= nil)
}

if let y2: Int? = x? {
    println("y2 = \(y2)")
}

if let y3: Int = x? {
    println("y3 = \(y3)")
}

if let y4: Int = x {
    println("y4 = \(y4)")
}

if let y5 = x? {
    println("y5 = \(y5)")
}

if let y6 = x {
    println("y6 = \(y6)")
}

结果(仅限)

"y1 = nil"

建议在 y1 案例中没有检查 x 的内部(并且 x 保留为包裹的 nil ,这不等于解包 nil )。 y2 案例似乎通过强制检查内部(或只是可选链接接管)来证实这一点;但是由于 y4 y6 的情况也不会打印,因此必须有更多的故事,因此表现得好像一个检查内部正在发生。

suggesting that no checking "inside" of x is taking place in the y1 case (and that x is left as a wrapped nil, which is not equal to unwrapped nil). The y2 case seems to confirm this by forcing a "check inside" (or is that just optional chaining "taking over"); but there must be more to the story since the y4 and y6 cases also do not print and thus behave as if a "check inside" is happening.

我怀疑从尝试中获得了一些洞察力

I suspect that there's some insight to be gained from trying

"x = 42"

导致

"y1 = Optional(42)"
"y2 = Optional(42)"
"y3 = 42"
"y4 = 42"
"y5 = 42"
"y6 = 42"

但是如果那里有三个,那就输给我了。

but if three's some there, it's lost on me.

似乎(1)如果要求进行明确检查,表达式右侧的可选确实会检查内部(使用 )?;但除此之外(2)表达式的左侧影响检查执行的内部程度(足以进行有效分配)。

It seems like (1) the "optional" on the right side of the expression does indeed get "checked inside" if an explicit check is asked for (with ?); but otherwise (2) the left hand side of the expression influences how far "inside" a check is performed (just far enough to make a valid assignment).

如何在每种情况下都可以选择绑定吗?特别是,当 x == nil 为什么 y1 打印时,如果确实如此,为什么不 y4 y6 (或生成分配错误)?

How is optional binding working in each of these cases? In particular, when x == nil why does y1 print, and given that it does, why don't y4 and y6 (or generate assignment errors)?

推荐答案

我的解释不同:

var x: Int? = 1

if let y1: Int = x {
    println("y1 = \(y1)") 
}

//prints y = 1, the optional was checked, contains a value and passes it

var x: Int? = nil

if let y1: Int = x {
    println("y1 = \(y1)") 
}

//does not execute because x does not contain value that can be passed to a non optional y

var x: Int? = nil

if let y1: Int? = x {
    println("y1 = \(y1)")
}
// y = nil, since y is optional and can hold a value of x which is nil, then it passes nil

可选绑定用于检查可选项是否包含要传递给非可选参数的值。

Optional binding is for checking if an optional contains a value to pass to a non optional parameter.

这篇关于为什么Swift的可选绑定在某些情况下会以'nil'成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆