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. Useful Online Resources for New Hackers

    出处:https://www.hackerone.com/blog/resources-for-new-hackers HackerOne喜欢花时间与活跃的黑客和有兴趣学习如何破解的人交谈. 就在上周 ...

  2. 公共语言运行库(CLR)开发系列课程(2):Pinvoke 进阶 学习笔记

    上一章地址 API版本 具有字符串参数的API通常有两种版本 GetWindowText GetWindowTextA GetWindowTextW 缺省情况下CLR会自动寻找合适的匹配 CharSe ...

  3. WPF UI 开源专贴

    1.ReactiveUI https://github.com/reactiveui/ReactiveUI http://www.reactiveui.net A MVVM framework tha ...

  4. 测试开发之Django——No2.Django的安装以及项目创建

    开发平台:Mac Python版本:3.7 Django版本:2.0.5 一.Django的安装 1.pip安装 输入命令pip install Django==2.0.5 说明:不指定版本,则安装的 ...

  5. SSD安装记录

    这两天配置SSD,折腾了一两天,终于搞定了,记录下自己遇到的大坑. 1.安装SSD 安装参考:http://blog.csdn.net/shawncheer/article/details/53227 ...

  6. VS Code折腾记 - (4) 常用必备插件推荐【前端】

    前言 这篇文章只要让你做一些基础的配置,把vscode变得更加顺手: 插件的需求不是一成不变,有些插件我已经移除了..在最新的VSCODE 1.9.1中, 部分以前用插件实现的功能已经集成了,那就没有 ...

  7. 使用CSS3改变文本选中的默认颜色

    ::selection { background:#d3d3d3; color:#555; } ::-moz-selection { background:#d3d3d3; color:#555; } ...

  8. 搭建项目vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize

    vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize https://www.cnblogs.com/wuguanglin ...

  9. chrome书签(收藏栏)的导入导出

  10. HTML、CSS、JS 样式 (未整理)

    随手记,有错误的地方希望留言 ^.-.^ PHP 实现关闭浏览器窗口echo "<script>window.close();</script>"; jqu ...