Optional是以enum和泛型为基础的高阶类型
2024-09-05 06:50:35
结论:1、optionals使用时需要检查;2、可以通过!+赋值语句转化为非optionals。
Optional-Generic Enumeration
enum
Optional<T> : LogicValue, Reflectable {
case
None
case
Some(T)
init()
init(_ some: T)
/// Allow use in a Boolean context.
func getLogicValue() -> Bool
/// Haskell's fmap, which was mis-named
func map<U>(f: (T) -> U) -> U?
func getMirror() -> Mirror
}
A type that represents either a wrapped value or nil
, the absence of a value.
The types of shortForm
and longForm
in the following code sample are the same:
let shortForm: Int? = Int("42")
let longForm: Optional<Int> = Int("42")
Optionals
?!
You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all.
optionals indicate that a constant or variable is allowed to have “no value”. Optionals can be checked with an if
statement to see if a value exists, and can be conditionally unwrapped with optional binding to access the optional’s value if it does exist.
let possibleString: String? = "An optional string."
let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // requires an exclamation mark
最新文章
- canvas 学习
- win7下利用VM8安装CentOS6.3配置静态IP上网
- IO-01. 表格输出(5)
- 读javascript高级程序设计15-Ajax,CORS,JSONP,Img Ping
- 030. asp.net中DataList数据绑定跳转(两种方式)的完整示例
- 各种工具使用手册:http://www.itshouce.com.cn/linux/linux-tcpdump.html 关于tcpdump!!!!
- Tomcat 部署:工程下 META-INF 目录下的 Context.xml
- UVa10025-The ? 1 ? 2 ? ... ? n = k problem
- TAT 前端突击队 第四关 题目 腐蚀的画
- html5 canvas 钟表
- 接水问题【NOIP2010普及组】优先队列
- Swift基础之Demo包含刷新,加载,网络请求,MVC
- 20165234 《Java程序设计》实验一 Java开发环境的熟悉
- git与eclipse集成之添加.gitignore文件
- Excel公式笔记
- Spring 的@@Autowired 和 @Qualifier注释
- strcore.cpp(156) 内存泄漏
- keras embeding设置初始值的两种方式
- 2018/04/04 PHP 中的 数组排序问题
- 让前端独立于后端进行开发,模拟数据生成器Mock.js
热门文章
- 限制input 内部字数
- struts2学习之基础笔记4
- json字符串与json对象的相互转换
- jqury中$(";#div";).index($this)在setTimeoutt中返回值一直是-1的问题解决方案
- Date.getTime() 结果为 NaN
- Unity 指定区域随机实例化预制体Prefab 代码
- CorelDRAW 2018新增功能介绍
- C# 基础复习 三 XML操作
- [luogu2501 HAOI2006] 数字序列 (递推LIS)
- C++函数传递数组的两种方式