作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的印象是,隐式解包的可选值在使用时会导致运行时异常,并且它为零。但是下面的代码可以正常运行,没有运行时异常,为什么?
var str:String?
println(str!) // Crashes as expected
var str:String! // Implicitly unwrapped
println(str) // Does not crash, not what I expect - it prints nil
最佳答案
它将变量打印为枚举(即可选),因为内部可选是 enum Optional<T>
。更准确地说,我认为它使用 debugDescription
属性,实际上发生的情况是这样的:
var str:String?
println(str) // Prints "nil"
str.debugDescription // Prints "nil"
关于swift - 隐式解包的可选值为 nil,但不会导致运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612254/
我是一名优秀的程序员,十分优秀!