?与!的区别

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. 微信小程序分享朋友圈的实现思路与解决办法

    实现思路 那么既然小程序没有分享到朋友圈的api,我们怎么实现分享到朋友圈呢,下面我介绍一下实现思路. 既然没有捷径,那就走复杂一点的路线,那就是需要用户手动分享到朋友圈,问题又来了,用户手动分享的话 ...

  2. 用replaceState操作路由的方法封装

    export class Router { ReplaceState(url, data) { var query = this.Generate(data); window.history.repl ...

  3. jquery应用实例1:手风琴特效

    效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  4. 对ListView的Item子控件监听并跳转页面

    public class MyAdapteforOwner extends BaseAdapter{ List<OwnerDevice>datas; private Context con ...

  5. win7系统桌面上图标都变成lnk后缀

    1.右键点击空白处,选择“新建”,点击“文本文档”: 2.将文档命名为“1”,后缀名改为inf: 3.双击打开,复制以下内容: [Version] Signature="$Chicago$& ...

  6. ZBrush中绘制层是什么意思?

    我们经常使用笔刷雕刻模型,在使用笔刷为头部模型添加一些纹理效果时,有时可能会有不满意的地方,但是很难修改,也很难把它还原为原来的状态,这时我们就可以使用Layers(绘制层)来将雕刻的部分分到每一个层 ...

  7. postgressql sql查询拼接多个字段为一个字段查询出来

    表年份 月份 数据1 数据22000 1 1 12000 2 2 12001 2 2 2 2001 5 5 4 希望的查询结果如下所示: 时间 数据1 数据22000年1月 1 12000年2月 2 ...

  8. jquery 用于操作动态元素的delegate/on方法

    delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素). 在做项目中有很多由ajax动态生成的html标签,jquery对这些标签不会响应\((selector).c ...

  9. myEclies项目导入Eclipse中常见问题

    需要配置Tomcat 左上方菜单 windosw > Prefrences TomCat JS文件报错 打开项目位置 找到 这个文件打开 删除这一段ok

  10. 鸟哥的linux私房菜

    http://vbird.dic.ksu.edu.tw/linux_basic/linux_basic.php