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? = ...
随机推荐
- Nginx是什么,有什么优点?为什么选择Nginx做web服务器软件?(经典经典)
1.基础知识 代理服务器: 一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器. 一个完整的代理请求过程为:客 ...
- git使用常用命令
第一部分:个人整理部分(读<Git教程By廖雪峰.pdf>笔记) /* 配置全局参数 */git config --global user.name "username" ...
- .NET C# Tostring format 格式化字符串
一.数值型 formatCode 是可选的格式化代码字符串.必须用“{”和“}”将格式与其他字符分开.如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”. ...
- 『实践』VirtualBox 5.1.18+Centos 6.8+hadoop 2.7.3搭建hadoop完全分布式集群及基于HDFS的网盘实现
『实践』VirtualBox 5.1.18+Centos 6.8+hadoop 2.7.3搭建hadoop完全分布式集群及基于HDFS的网盘实现 1.基本设定和软件版本 主机名 ip 对应角色 mas ...
- jquery-easyui:如何设置组件属性
在这里以面板为例: $().ready(function() { $('#menu').tree({ url : '/menu', onClick : function(node) { $('#cen ...
- 乐视max2 在开发中无法打印某些logcat 解决方案
乐视屏蔽了打印log.d等类型logcat.解决方案:拨号键盘 *#*#76937#*#* 出现页面后选最下面那个选项就有了.
- Android 5.0 行为变更
Android 5.0 除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 如果您之前发布过 Android 应用,请注意 ...
- Data Visualization Books
Data Visualization: Principles and Practice
- 一些计数小Trick
一些计数小Trick 虽然说计数问题如果不是特别傻逼的话想做出来基本随缘. 但是掌握一些基本的计数方法还是十分有必要的. 想到了就更新. 1. 对于排列的DP问题,一般是不能够按照位置一个一个放的,一 ...
- day13--开发堡垒机
本节内容 项目实战:运维堡垒机开发 商业:<齐治--堡垒机> 前景介绍 https://www.cnblogs.com/alex3714/articles/ ...