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类型
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.
let roomCount = john.residence!.numberOfRooms// 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:
var testScores = ["Dave": [86, 82, 84], "Bev": [79, 94, 81]]testScores["Dave"]?[0] = 91testScores["Bev"]?[0] += 1testScores["Brian"]?[0] = 72// 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的更多相关文章
- Swift中可选型的Optional Chaining 和 Nil-Coalesce(Swift2.1)
/* 下面是介绍Optional Chaining 和 Nil-Coalesce */ // Optional Chaining (可选链) if let errorMessage = errorMe ...
- Welcome-to-Swift-17自判断链接(Optional Chaining)
自判断链接(Optional Chaining)是一种可以请求和调用属性.方法及子脚本的过程,它的自判断性体现于请求或调用的目标当前可能为空(nil).如果自判断的目标有值,那么调用就会成功:相反,如 ...
- Swift Optional Chaining
Optional Chaining介绍 关于「optional chaining」,<The Swift Programming Language>是这么描述的: Optional cha ...
- [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 ...
- 精读《Optional chaining》
1. 引言 备受开发者喜爱的特性 Optional chaining 在 2019.6.5 进入了 stage2,让我们详细读一下草案,了解一下这个特性的用法以及讨论要点. 借着这次精读草案,让我们了 ...
- js optional chaining operator
js optional chaining operator js 可选链 可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效. ?. 操作符的功能类似于 ...
- TypeScript 3.7 RC & Optional Chaining
TypeScript 3.7 RC & Optional Chaining https://devblogs.microsoft.com/typescript/announcing-types ...
- TypeScript 中 Optional Chaining 和 Nullish Coalescing
Optional Chaining 解决的问题是重复且无意义的判空,之所以说无意义,是对业务来说它不是必需的,但不判空,程序直接就挂了,比如: let x = foo.bar.baz(); 这里的 ...
- Swift-09-可空链式调用(Optional Chaining)
我对这个的理解就是:我们有可能会用到其他的属性或者方法,当我们在使用其他的时候,可以使用点语法去访问另一个的属性,这样的使用,就形成了链式访问. 可空链式调用是一种可以请求和调用属性.方法及下表的过程 ...
随机推荐
- 安装wampserver遇到的问题及解决方案
丢失api-ms-win-crt-runtime-l1-1-0.dll 安装完wampserver,启动服务器的时候遇到一些问题,提示说缺失dll文件,如下图所示: 网上一搜,很多人出现过丢失api- ...
- Visual Studio icon 含义
图片摘自:https://msdn.microsoft.com/en-us/library/y47ychfe.aspx
- selenium基础
浏览器 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转.输入.点击.下拉等来拿到网页渲染之后的结果,可支持多种浏览器 官网链接:http://selenium-python.re ...
- MySQL_索引原理与慢查询优化
索引原理与慢查询优化 创建/删除索引的语法 #方法一:创建表时 CREATE TABLE 表名 ( 字段名1 数据类型 [完整性约束条件…], 字段名2 数据类型 [完整性约束条件…], [UNIQU ...
- JavaScript学习笔记(第二天)
数组 为什么要学习数组 之前学习的数据类型,只能存储一个值(比如:Number/String.我们想存储班级中所有学生的姓名,此时该如何存储? 数组的概念 所谓数组,就是将多个元素(通常是同一类型)按 ...
- HDU 2078 选课时间( 水题 )
链接:传送门 思路:水题略 /************************************************************************* > File N ...
- 【技术翻译】SIFT算子原理及其实现 (一)介绍
介绍 匹配不同图片的特征是计算机视觉常见的问题. 当所有要匹配的图片很相似的时候(大小,方位),简单的角点检测算子就可以匹配,但是,当你的图片大小,方位不同的时候,你就要用到尺度不变特征变换(scal ...
- python3使用selenium3的坑
网络看了很多的文章,大部分都是不完整, 还有很多误导性极强的教程 ,特别是chromedriver这东西.简直一堆坑. 一首先是安装python3.6.5 root@ubuntu:~# add-apt ...
- JSp获取到当前用户的全部session
<%@page import="java.util.Enumeration"%> <% for (Enumeration<?> e = session ...
- where和having
where可以不能使用别名作为过滤条件,而having可以使用别名作为过滤条件. 在ORACLE中,select 语句的执行顺序是: 1. from语句 2. where语句(结合条件) 3. sta ...