本文介绍了在Apple Swift中,在什么情况下我不想要一个隐式解包的可选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我理解为什么选项很方便(我最好的想法是能够返回一个nil布尔值),但在什么情况下我想声明一个包装的可选用?而不是 !对于隐式展开的可选项。

I believe I understand why optionals are handy (my best thought for use is to be able to return a nil Boolean value), but in what case would I want to declare a wrapped optional using ? rather than ! for an implicitly unwrapped optional.

似乎没有必要用它来声明它?然后必须打字!到处都是,而不仅仅是使用!一次。

It just seems unnecessary to declare it with ? and then have to type ! all over the place rather than just using ! once.

我不想忽视?虽然没用,但我找不到它的用途...任何想法?

I don't want to disregard the ? as useless, but I just can't find a use for it... Any ideas?

推荐答案

如果你试图访问隐式展开的可选项的内容,并且没有任何内容,您的应用程序将崩溃。

If you try to access the content of an implicitly unwrapped optional and there's nothing there, your app will crash.

如果您使用模式检查可选内容 - 例如 - 您可以控制应用程序在不可预见的情况下如何正常失败。并且它不会使你的代码变得更加复杂。

If you use the patterns for checking the content of an optional — like optional binding and optional chaining - you can control how your app should fail gracefully in unforeseen situations. And it doesn't make your code all that more complicated.

不崩溃对我来说似乎是一个很好的理由。

Not crashing seems like a good reason to me.

这篇关于在Apple Swift中,在什么情况下我不想要一个隐式解包的可选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:13