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的更多相关文章

  1. [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 ...

  2. js Nullish Coalescing Operator

    js Nullish Coalescing Operator 空值合并 const n = null ?? 'default string'; console.log(n); // "def ...

  3. Null Coalescing Operator

    w Parse error: syntax error, unexpected '?'

  4. 运算符 swift

    1nil聚合运算符 nil coalescing operator a ?? b ==>a!=nil ? a! : b 要求: 1a是一个可选类型 2b必须和a解包后类型一致 var userN ...

  5. Swift-2-基本操作符

    // Playground - noun: a place where people can play import UIKit // 基本运算符 // 运算符有3种: 单目运算符(如 -a),二目运 ...

  6. swift基本运算符

    一.空合运算符(Nil Coalescing Operator) 形式:a??b,如果a包含值则解封,否则返回默认值b 条件:a必须为optional类型,这个就不多说了,就是可选类型:默认值b的类型 ...

  7. Swift 版本历史记录(关注)

    http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapter1/03_revision_history.ht ...

  8. Swift 学习笔记 (一)

    原创: 转载请注明出处 Extention try catch rxSwift internal  public  private var  let as       as? 强转 ? ! didSe ...

  9. Swift中空合运算符、闭区间运算符、单侧区间、半开区间

    空合运算符(Nil Coalescing Operator) 用于取代3目判空运算,提供超短的写法比如常规判空写法如下,反正我写java就是这么干的 var anOptionalInt: Int? = ...

随机推荐

  1. Linux驱动技术(五) _设备阻塞/非阻塞读写【转】

    转自:http://www.cnblogs.com/xiaojiang1025/p/6377925.html 等待队列是内核中实现进程调度的一个十分重要的数据结构,其任务是维护一个链表,链表中每一个节 ...

  2. MySQL 5.7在线设置复制过滤【转】

    转自 MySQL 5.7在线设置复制过滤 - yayun - 博客园 https://www.cnblogs.com/gomysql/p/4991197.html 5.7也GA了,有许多新的特性,其中 ...

  3. scrapy主动触发关闭爬虫

    在spider中时在方法里直接写 self.crawler.engine.close_spider(self, 'cookie失效关闭爬虫')   在pipeline和downloaderMiddle ...

  4. springcloud Zuul中异常处理细节

    Spring Cloud Zuul对异常的处理整体来说还是比较方便的,流程也比较清晰,只是由于Spring Cloud发展较快,各个版本之间有差异,导致有的小伙伴在寻找这方面的资料的时候经常云里雾里, ...

  5. elasticsearch(ES)日志迁移

    =============================================== 2018/7/29_第1次修改                       ccb_warlock == ...

  6. 关于sudo 权限被修改的解决方法

    在用sudo安装文件的时候,出现如下错误提示: sudo: /etc/sudoers is world writable sudo: no valid sudoers sources found, q ...

  7. logback.xml 模板

    ssm模板 <?xml version="1.0" encoding="UTF-8"?>  <!--configuration 根节点,包含下 ...

  8. opencv SVM多分类 人脸识别

    上一篇介绍了OPENCV中SVM的简单使用,以及自带的一个二分类问题. 例子中的标签是程序手动写的,输入也是手动加的二维坐标点. 对于复杂问题就必须使用数据集中的图片进行训练,标签使用TXT文件或程序 ...

  9. conda管理python包

    参考:http://blog.sina.com.cn/s/blog_c3c116470102wlv5.html 查看python,numpy,scipy,matplotlib的版本及安装位置: htt ...

  10. LOJ 10160 - 「一本通 5.2 练习 3」周年纪念晚会 / 没有上司的晚会

    题面 传送门 Ural 州立大学的校长正在筹备学校的 8080 周年纪念聚会.由于学校的职员有不同的职务级别,可以构成一棵以校长为根的人事关系树.每个资源都有一个唯一的整数编号,从 $1$ 到 $N$ ...