nil coalescing operator
nil coalescing operator ??
就是 optional和 三元运算符?:的简写形式。
比如一个optional String类型的变量
var a:String?
// println(a != nil ? a! : "shabi")
println(a ?? "shabi") // shabi
// a ? ? "shabi" equals a != nil ? a! : "shabi"
a = "hecheng"
println(a ? ? "shabi") // hecheng
// 这个运算符加入于2014-08-04 (The Swift Programming Language Revision History)
AFNetWorking的创始人 Mattt Thompson在他的个人博客里就曾用过这个运算符:
http://nshipster.com/swift-literal-convertible/
struct Set<T: Hashable> {
typealias Index = T
private var dictionary: [T: Bool] init() {
self.dictionary = [T: Bool]()
} var count: Int {
return self.dictionary.count
} var isEmpty: Bool {
return self.dictionary.isEmpty
} func contains(element: T) -> Bool {
return self.dictionary[element] ?? false // 这里
} mutating func put(element: T) {
self.dictionary[element] = true
} mutating func remove(element: T) -> Bool {
if self.contains(element) {
self.dictionary.removeValueForKey(element)
return true
} else {
return false
}
}
}
由于字典通过['key']获取后是一个Optional类型,假设这个key相应的value不存在的话,能够通过??
运算符设置一个不存在时的默认值(false)。
nil coalescing operator的更多相关文章
- [TypeScript ] Using the Null Coalescing operator with TypeScript 3.7
The postshows you how to use the null coalescing operator (??) instead of logical or (||) to set def ...
- js Nullish Coalescing Operator
js Nullish Coalescing Operator 空值合并 const n = null ?? 'default string'; console.log(n); // "def ...
- Null Coalescing Operator
w Parse error: syntax error, unexpected '?'
- 运算符 swift
1nil聚合运算符 nil coalescing operator a ?? b ==>a!=nil ? a! : b 要求: 1a是一个可选类型 2b必须和a解包后类型一致 var userN ...
- Swift-2-基本操作符
// Playground - noun: a place where people can play import UIKit // 基本运算符 // 运算符有3种: 单目运算符(如 -a),二目运 ...
- swift基本运算符
一.空合运算符(Nil Coalescing Operator) 形式:a??b,如果a包含值则解封,否则返回默认值b 条件:a必须为optional类型,这个就不多说了,就是可选类型:默认值b的类型 ...
- Swift 版本历史记录(关注)
http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapter1/03_revision_history.ht ...
- Swift 学习笔记 (一)
原创: 转载请注明出处 Extention try catch rxSwift internal public private var let as as? 强转 ? ! didSe ...
- Swift中空合运算符、闭区间运算符、单侧区间、半开区间
空合运算符(Nil Coalescing Operator) 用于取代3目判空运算,提供超短的写法比如常规判空写法如下,反正我写java就是这么干的 var anOptionalInt: Int? = ...
随机推荐
- mysql innobackupex 备份及恢复
----------------------------------全量备份恢复-------------------------------------1.生成一个完整的备份 innobackupe ...
- Java中获取包含变量的配置文件config.properties内容
应用场景 有些时候项目中会用到很多路径,并且很可能多个路径在同一个根目录下,那为了方便配置的修改,达到只修改根目录即可达到一改全改的效果,此时就会想到要是有变量就好了: 另外有时候路径中的文件名是不确 ...
- Web框架的原理
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. socket服务端 import ...
- kali linux2.0安装vega
1.到官网下载安装包(选择版本:我这里以Linux64位为例) vega下载地址:https://subgraph.com/vega/download/ 2.解压到指定目录: unzip VegaBu ...
- 【linux】sed -e 's/-//g'
sed是用来处理文本的 s/正则表达式/替换字符串/ :表示将正则表达式的内容替换为后面的字符串 g :表示替换全部,即如果不加g,则只会替换第一个 -e :多点编辑(这个没懂) 例 ...
- Django Celery定时任务和时间设置
1.Celery加入定时任务 Celery除了可以异步执行任务之外,还可以定时执行任务.在实例代码的基础上写个测试方法: #coding:utf- from celery.task.schedules ...
- 说一下PHP中die()和exit()区别
PHP手册:die()Equivalent to exit(). 说明:die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别 ...
- JS实现集合和ECMA6集合
集合类似于数组,但是集合中的元素是唯一的,没有重复值的.就像你学高中数学的概念一样,集合还可以做很多比如,并集,交集,差集的计算.在ECMA6之前,JavaScript没有提供原生的Set类,所以只能 ...
- VirtualBox 安装 Gentoo 小记
因为需求,尝试了一下在 VirtualBox 安装 Gentoo.虽然多年前就折腾过多次 LFS,但 Gentoo 并没有太多尝试.这次确实也经历了种种波折,到最后总算成功了,大致跨度为3天.本来手上 ...
- .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录
结合前面封装的ExceptionLess,接下来使用 AspectCore 实现AOP日志处理 nuget导入AspectCore.Core .AspectCore.Extensions.Depend ...