if convertedNumber != nil { println("convertedNumber has an integer value of \(convertedNumber!).") } // prints "convertedNumber has an integer value of 123."
有了这个解释
Once you’re sure that the optional does contain a value,you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. The exclamation mark effectively says,“I know that this optional definitely has a value; please use it.” This is known as forced unwrapping of the optional’s value:
好的,明白了,但它的用处是什么?如果我没有强行拆包,那就不一样了:
if convertedNumber != nil { println("convertedNumber has an integer value of \(convertedNumber).") } // prints "convertedNumber has an integer value of 123."
感谢启发我:)
Wouldn’t be the same if I didn’t forced the unwrapping
不可以.可选是另一种类型.如果您实际上在没有展开的情况下运行上面的代码(在操场上这是微不足道的),您可以立即看到差异.
可选打印为可选(123),打开打印为123.您无法将可选项传递给需要特定类型的内容而无需先将其打开.
理解Optionals是完全不同的类型,而不是特定类型的特殊类型(例如,没有“可选的Int”,有一个包含Int的Optional)是理解这一点的关键.如果你对更长的解释感兴趣,我已经写了here.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。