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. 【ARTS】01_07_左耳听风-20181224~1230

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  2. asp.net动态增加服务器端控件并提交表单

    为什么要用原生的呢? 1.目的 原生出现浏览器兼容性问题 极少,不用测试多浏览兼容性 .需要考虑到市面上的其他垃圾浏览器. 2.性能不好 如果不考虑第一条 你可以换一种方式 直接上代码 .aspx页面 ...

  3. C# XML 文档注释

    原文链接:http://www.shinater.com/DocsBuilder/help.html <summary>description</summary> 描述类型或类 ...

  4. C++获取文件夹下所有文件名

    查找文件需要一个结构体和几个函数.结构体为struct _finddata_t,函数为_findfirst.findnext和_findclose. struct _finddata_t 这个结构体是 ...

  5. elasticsearch分别在windows和linux系统安装

    WINDOWS系统安装1.安装JDKElastic Search要求使用较高版本JDK,本文使用D:\DevTools\jdk1.8.0_131,并配置环境变量 2.安装Elastic Search官 ...

  6. 2012 Dhaka

    2012 Dhaka B - Wedding of Sultan 题目描述:给出一棵树的\(dfs\)序(只要经过就会记录),求每个点的度 solution 按\(dfs\)序的规则还原这棵树就好了. ...

  7. 关于iTerm2中颜色配置及快捷键使用技巧(亲测)

    https://github.com/mbadolato/iTerm2-Color-Schemes http://chriskempson.com/projects/base16 (同事用的) 按照g ...

  8. Nginx安装方式探究

    Ubuntu 16.04(阿里云ECS),Nginx 1.10.3 (Ubuntu) 本文探究两种安装方式: 1.源码安装(手动) 2.APT安装(自动) 源码安装(手动) 步骤简介: 下载.解压.. ...

  9. HTTPS-加密SSL证书

    从第一部分HTTP工作原理中,我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密码.在握手过程中,网站会向浏览器发送SSL证书,SSL证书和我们日常用的身份证类似 ...

  10. 【转】MySQL安全配置介绍

    一.前言 很多文章中会说,数据库的权限按最小权限为原则,这句话本身没有错,但是却是一句空话.因为最小权限,这个东西太抽象,很多时候你并弄不清楚具体他需要哪些权限. 现在很多mysql用着root账户在 ...