?与!的区别

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类型

To reflect the fact that optional chaining can be called on a nil value, the result of an optional chaining call is always an optional value,

Specifically, the result of an optional chaining call is of the same type as the expected return value, but wrapped in an optional.

  1. let roomCount = john.residence!.numberOfRooms
  2. // this triggers a runtime error

Accessing Subscripts Through Optional Chaining

You can use optional chaining to try to retrieve and set a value from a subscript on an optional value, and to check whether that subscript call is successful.

Accessing Subscripts of Optional Type

If a subscript returns a value of optional type—such as the key subscript of Swift’s Dictionary type—place a question mark after the subscript’s closing bracket to chain on its optional return value:

  1. var testScores = ["Dave": [86, 82, 84], "Bev": [79, 94, 81]]
  2. testScores["Dave"]?[0] = 91
  3. testScores["Bev"]?[0] += 1
  4. testScores["Brian"]?[0] = 72
  5. // the "Dave" array is now [91, 82, 84] and the "Bev" array is now [80, 94, 81]

Optional Chaining as an Alternative to Forced Unwrapping的更多相关文章

  1. Swift中可选型的Optional Chaining 和 Nil-Coalesce(Swift2.1)

    /* 下面是介绍Optional Chaining 和 Nil-Coalesce */ // Optional Chaining (可选链) if let errorMessage = errorMe ...

  2. Welcome-to-Swift-17自判断链接(Optional Chaining)

    自判断链接(Optional Chaining)是一种可以请求和调用属性.方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(nil).如果自判断的目标有值,那么调用就会成功:相反,如 ...

  3. Swift Optional Chaining

    Optional Chaining介绍 关于「optional chaining」,<The Swift Programming Language>是这么描述的: Optional cha ...

  4. [TypeScript] Optional Chaining with TypeScript 3.7

    TypeScript 3.7 adds support for optional chaining. This lesson shows you how to use it in your code ...

  5. 精读《Optional chaining》

    1. 引言 备受开发者喜爱的特性 Optional chaining 在 2019.6.5 进入了 stage2,让我们详细读一下草案,了解一下这个特性的用法以及讨论要点. 借着这次精读草案,让我们了 ...

  6. js optional chaining operator

    js optional chaining operator js 可选链 可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效. ?. 操作符的功能类似于 ...

  7. TypeScript 3.7 RC & Optional Chaining

    TypeScript 3.7 RC & Optional Chaining https://devblogs.microsoft.com/typescript/announcing-types ...

  8. TypeScript 中 Optional Chaining 和 Nullish Coalescing

    Optional Chaining 解决的问题是重复且无意义的判空,之所以说无意义,是对业务来说它不是必需的,但不判空,程序直接就挂了,比如: let x = foo.bar.baz();   这里的 ...

  9. Swift-09-可空链式调用(Optional Chaining)

    我对这个的理解就是:我们有可能会用到其他的属性或者方法,当我们在使用其他的时候,可以使用点语法去访问另一个的属性,这样的使用,就形成了链式访问. 可空链式调用是一种可以请求和调用属性.方法及下表的过程 ...

随机推荐

  1. visio中如何取消跨线和去掉页边距

    比较来说,写论文visio和inkscape都不可缺少. 比如visio跨线的问题,已经遇到过两次忘记了.这次截个图作为记录.其实就是在“设计”一栏里,把连接线里面的跨线显示的对勾去掉即可. *** ...

  2. DirectUI界面编程(六)实现右键弹出菜单

    本节向大家介绍一下右键弹出菜单是如何实现的.效果如下,在窗口中点击鼠标右键弹出菜单,点击菜单项能够响应菜单点击事件. 使用Duilib库实现的弹出菜单,实际上也是一个Windows窗口,因此我们需要创 ...

  3. ndis6 how to drop packets

    In ndis6 how to drop packets? in FilterSendNetBufferLists: FILTER_RELEASE_LOCK(&pFilter->Lock ...

  4. pixhawk入门知识

    Pixhawk是一种先进的自动驾驶仪,由PX4开放硬件项目设计和3D机器人制造.它具有来自ST公司先进的处理器和传感器技术,以及NuttX实时操作系统,能够实现惊人的性能,灵活性和可靠性控制任何自主飞 ...

  5. LayUI中select下拉框选中触发事件

    代码: var form = layui.form, layer = layui.layer; // 监听 $(document).ready(function() { // select下拉框选中触 ...

  6. webpack安装,npm WARN optional SKIPPING OPTIONAL DEPENDENCY,npm WARN notsup SKIPPING OPTIONAL DEPENDENCY警告

    npm install webpack -g//全局安装webpack 电脑上安装完后: 其中有两个警告: npm WARN optional SKIPPING OPTIONAL DEPENDENCY ...

  7. 如何保证 Linux 服务器的安全

    如何保证 Linux 服务器的安全 2013/09/17 | 分类: IT技术 | 0 条评论 | 标签: LINUX, 服务器 分享到:53 本文由 伯乐在线 - 贾朝藤 翻译自 Spenser J ...

  8. Windows 10快速在指定目录打开命令行

    一.我们在想要到达的目录上按住shift键并点击鼠标右键.看到了吗,这时候在弹出菜单里多了一个选项,就是"在此处打开命令窗口",我们点开看一下. 二.不过有时候我们需要以管理员的权 ...

  9. 小学生绞尽脑汁也学不会的python(面对对象-----类与类之间的关系)

    小学生绞尽脑汁也学不会的python(面对对象-----类与类之间的关系 1. 依赖关系. 最轻的一种关系 在方法中引入另一个类的对象 class Elephant: def __init__(sel ...

  10. ASP.NET-后台cookie与前台JQUERY解析cookie

    在controller中给cookie赋值 HttpCookie cookie =newHttpCookie("pageInfo"); cookie["page_inde ...