xcode 之间的技术区别是什么?&!用快速的语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24352207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:07:11  来源:igfitidea点击:

What is the technical difference between ? & ! in swift language?

iosiphonexcodeswiftios8

提问by Naveen

Can anyone explain in detail about the technical difference between ! & ? literals that we are using in swift language. Basically in apple developer document they mentioned it as the nil validation but i didn't understand clearly about the logic behind it. Can you one explain with some real time logic for the same.

谁能详细解释一下两者之间的技术差异!& ? 我们在 swift 语言中使用的文字。基本上在苹果开发人员文档中,他们将其称为 nil 验证,但我并不清楚其背后的逻辑。你能用一些实时逻辑来解释一下吗?

Thanks in advance for the user.

在此先感谢用户。

回答by Tommy

Optional Chaining as an Alternative to Forced Unwrapping

You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. This is very similar to placing an exclamation mark (!) after an optional value to force the unwrapping of its value. The main difference is that optional chaining fails gracefully when the optional is nil, whereas forced unwrapping triggers a runtime error when the optional is nil.

可选链作为强制展开的替代方法

如果可选项非零,您可以通过在您希望调用属性、方法或下标的可选项值后面放置一个问号 (?) 来指定可选项链。这与在可选值后放置感叹号 (!) 以强制展开其值非常相似。主要区别在于,当 optional 为 nil 时,可选链会优雅地失败,而当 optional 为 nil 时,强制解包会触发运行时错误。

The Swift Programming Language(emphasis added)

Swift 编程语言(强调)

Nobody's going to be able to explain it more simply than that. What don't you understand?

没有人能够比这更简单地解释它。你不明白什么?