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? = ...
随机推荐
- python版本管理工具pyenv和包管理工具pipenv
一.pyenv版本管理工具 pyenv是一个python版本管理工具,可以实现轻松切换多个python版本 它可根据每个用户更改全局python版本,也可以为每个项目指定python版本,还可以管理v ...
- Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7)【转】
原文地址:Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.c ...
- [转]使用 mitmproxy + python 做拦截代理
使用 mitmproxy + python 做拦截代理 本文是一个较为完整的 mitmproxy 教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 ...
- SpringMVC高级参数绑定(数组和List)
本节内容: 参数绑定之数组 将表单的数据绑定到List 复制下上篇博客中的工程,作为今天开发的工程. 一.参数绑定之数组 1. 需求 在商品列表页面选中多个商品,然后删除. 2. 需求分析 功能要求商 ...
- hdu 5443 (2015长春网赛G题 求区间最值)
求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...
- 主席树初步学习笔记(可持久化数组?静态区间第k大?)
我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...
- 自定义yum源
1.创建rpm包的存放目录 mkdir -p /yum/yum-sum/package 2.准备rpm包,可以通过自带yum只下载不安装工具下载 yum install --downloadon ...
- 【LOJ】#6432. 「PKUSC2018」真实排名
题解 简单分析一下,如果这个选手成绩是0,直接输出\(\binom{n}{k}\) 如果这个选手的成绩没有被翻倍,那么找到大于等于它的数(除了它自己)有a个,翻倍后不大于它的数有b个,那么就从这\(a ...
- linux {..}用法
一.2018-{01..07}* 这个意思是 2018-01* 到2018-07*全部输出出来. 二.echo {a..c} a b c
- P1616 疯狂的采药 洛谷
题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说 ...