在 Swift 中, !函数签名中的符号是什么意思? [英] In Swift, what does the ! symbol mean in a function signature?

查看:28
本文介绍了在 Swift 中, !函数签名中的符号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 函数签名中,参数后面的 ! 意味着什么?更具体地说,这是否意味着参数需要在传入之前解包,或者在传入时(自动)解包.这是一个例子:

In a Swift function signature, what does the ! after an argument imply? More specifically, does it mean the argument needs to be unwrapped before it is passed in or that it gets unwrapped (automatically) as it is passed in. Here is an example:

func annotationButtonTUI(sender: UIButton!) { }

在这种情况下,函数是 UIButton 的目标,因此 ! 发生的任何事情都会自动发生.

In this case the function is a target for a UIButton so whatever happens with the ! is happening automatically.

我的想法是这意味着你可以期待一个解包的 sender 对象,所以你不需要尝试解包它.

My thought is it means you can expect an unwrapped sender object so you don't need to try an unwrap it.

推荐答案

这并不完全是重复的——在函数签名中隐式解包的选项 有一些微妙之处,超出了它们在其他地方的使用.

This isn't quite a duplicate — there's some subtlety to implicitly unwrapped optionals in function signatures beyond their usage elsewhere.

您会在从 ObjC 导入的 API 中看到隐式解包的选项,因为这是预期存在但可以为零的对象的最接近的 Swift 近似值.这是对导入 API 的妥协——您可以像在 ObjC 中一样直接处理这些变量,并且您可以使用 Swift 可选语法测试它们是否为零.(在 WWDC14 的 高级互操作性演讲中详细介绍了 Apple 对此的基本原理.)这种模式也适用到接口生成器插入的 IBAction 声明,因为这些方法实际上也是从 ObjC 代码中调用的.

You see implicitly unwrapped optionals in API imported from ObjC because that's the closest Swift approximation of an object that's expected to be there but which can be nil. It's a compromise for imported API — you can address these variables directly like in ObjC, and you can test them for nil using Swift optional syntax. (There's more about Apple's rationale for this in the Advanced Interoperability talk from WWDC14.) This pattern also applies to the IBAction declarations inserted by Interface Builder, since those methods are in effect getting called from ObjC code, too.

正如您所怀疑的那样,Swift 在从 ObjC 桥接时将可能的 nil 包装在一个可选项中,但是您的函数实现声明中的 ! 会解开该值,以便您可以直接使用它.(风险自负.)

As you seem to have suspected, Swift wraps the possible nil in an optional when bridging from ObjC, but the ! in your function implementation's declaration unwraps the value so you can use it directly. (At your own risk.)

自 Swift 1.2(2015 年春季为 Xcode 6.2)以来,ObjC API 可以使用 nonnullnullable 进行注释,在这种情况下,这些 API 的 Swift 接口使用非可选类型或完全可选类型.(从 Swift 2.0/Xcode 7.0 开始,几乎所有 Apple 的 API 都经过审核以使用可空性注释,因此它们的 Swift 签名不再使用太多 !.)

Since Swift 1.2 (Xcode 6.2 in Spring 2015), ObjC APIs can be annotated with nonnull and nullable, in which case the Swift interface to those APIs uses either a non-optional type or a fully optional type. (And since Swift 2.0 / Xcode 7.0, nearly all of Apple's APIs are audited to use nullability annotations, so their Swift signatures don't use much ! anymore.)

关于这一点鲜为人知的是,当您实现自己的由 ObjC 调用的 Swift 函数时,您可以自由更改参数的可选性.如果你想让编译器强制你的 action 方法中的 sender 永远不能为 nil,你可以从参数类型中去掉 ! .如果您希望编译器确保您始终测试参数,请将 ! 更改为 ?.

What's less well-known about this is that you're free to change the optionality of parameters when you implement your own Swift functions that get called by ObjC. If you want the compiler to enforce that sender in your action method can never be nil, you can take the ! off the parameter type. If you want the compiler to make sure you always test the parameter, change the ! to a ?.

这篇关于在 Swift 中, !函数签名中的符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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