Swift Expressible literal
Swift Expressible Literal
引子
从一个面试题说起。有如下代码:
struct Book {
let name: String
}
let book: Book = "Charlotte's Web"
请问是否可以让它正常编译?
字面量
字面量指字面代表的常量值。这样解释起来有点像废话。但是通过例子就很容易理解了。
以下是 Swift 中的一些字面量:
| 种类 | 示例 |
|---|---|
| 字符串 | "ich", Bingo! |
| 浮点型数字 | 1001.3, 33.0 |
| 整型数字 | 1001, 33 |
| 布尔值 | true, false |
| 数组 | ["Emily", "Ben", "Eric"] |
| 字典 | ["name": "Ben"] |
| 空 | nil |
Expressible Literal 协议
Swift 中,String, Float, Double, Int 和 Bool 等类型,和上面的 Book 一样,都是 struct 。那么它们是怎么实现将字面量直接创建实例的呢?以下是 Bool 类型的一个拓展:
extension Bool : ExpressibleByBooleanLiteral {
/// Creates an instance initialized to the specified Boolean literal.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you use a Boolean literal. Instead, create a new `Bool` instance by
/// using one of the Boolean literals `true` or `false`.
///
/// var printedMessage = false
///
/// if !printedMessage {
/// print("You look nice today!")
/// printedMessage = true
/// }
/// // Prints "You look nice today!"
///
/// In this example, both assignments to the `printedMessage` variable call
/// this Boolean literal initializer behind the scenes.
///
/// - Parameter value: The value of the new instance.
public init(booleanLiteral value: Bool)
/// A type that represents a Boolean literal, such as `Bool`.
public typealias BooleanLiteralType = Bool
}
再看看 Float 的一个拓展:
extension Float : ExpressibleByIntegerLiteral {
/// Creates an instance initialized to the specified integer value.
///
/// Do not call this initializer directly. Instead, initialize a variable or
/// constant using an integer literal. For example:
///
/// let x = 23
///
/// In this example, the assignment to the `x` constant calls this integer
/// literal initializer behind the scenes.
///
/// - Parameter value: The value to create.
public init(integerLiteral value: Int64)
/// A type that represents an integer literal.
///
/// The standard library integer and floating-point types are all valid types
/// for `IntegerLiteralType`.
public typealias IntegerLiteralType = Int64
}
除了 ExpressibleByBooleanLiteral 和 ExpressibleByBooleanLiteral 类似的协议还有 ExpressibleByStringLiteral, ExpressibleByNilLiteral, ExpressibleByArrayLiteral 等等。
所以,最开始的那个题目,就有答案了。
注意上面两个大代码块里面的注释,都有一句 Do not call this initializer directly 。
Swift Expressible literal的更多相关文章
- 浅谈Swift集合类型
Swift 的集合表现形式由数组和字典组成.它可以完美的存储任何呢想存储的东西. 数组是一个同类型的序列化列表集合,它用来存储相同类型的不同值.字典也是一个数组,但它的存值方式类似于Map,通过一对一 ...
- 使用swift 中的注意,不断完善中
1. 应该充分利用swfit的新特性 比如如果按照oc里的习惯,调用一个delegate中都optional函数应该这样写 if delegate != nil && delegate ...
- Apple Swift编程语言入门教程
Apple Swift编程语言入门教程 作者: 日期: 布衣君子 2015.09.22 目录 1 简介 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 ...
- Swift声明参考
一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...
- Swift数据类型简介(二)
整数 整数就是没有小数部分的数字,比如42和-23.整数可以是有符号(正.负.零)或者无符号(正.零). Swift 提供了8,16,32和64位的有符号和无符号整数类型.这些整数类型和 C 语言的命 ...
- swift中文文档翻译之--字符串和字符
字符串和字符 A string is an ordered collection of characters, such as "hello, world" or "al ...
- swift中文文档- 类型转换
未翻译完 待续(英语烂,求斧正) Type Casting 类型转换 Type casting is a way to check the type of an instance, and/or to ...
- swift:类型转换(is用作判断检测、as用作类型向下转换)
类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. 类型转换在Swift中使用is 和 as操作符实现.这两个操作符提供了一种简单达意的方式去检查值的类型或者转换 ...
- Swift学习笔记五
基础运算符 Swift的大部分运算符和C及OC相同,也分一元二元多元的,这里只记录一些Swift特有的性质或写法. 赋值运算符( = ) 在等号右边是一个有多个值的元组时,它的成员值可以分解并同时分别 ...
随机推荐
- CF 878E Numbers on the blackboard 并查集 离线 贪心
LINK:Numbers on the blackboard 看完题觉得很难. 想了一会发现有点水 又想了一下发现有点困难. 最终想到了 但是实现的时候 也很难. 先观察题目中的这个形式 使得前后两个 ...
- Servlet容器启动过程
参考:https://blog.csdn.net/fredaq/article/details/9366043 一.概念 所谓Servlet容器其实说白了是符合Servlet规范的Java web容器 ...
- RabbitMQ学习总结(1)-基础概念
1. 概念 1.1 AMQP协议 AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消 ...
- ElasticSearch添加索引
1. 编写索引内容 节点解释: settings:配置信息 "number_of_replicas": 0 不需要备份(单节点的ElasticSearch使用) "ma ...
- MySQL的utf8问题
作者:brightwang 原文:https://www.jianshu.com/p/ab9aa8d4df7d 有时候用MySQL存储一些特殊字符时,有出现乱码问题. 我用的是UTF-8编码的客户端, ...
- 强烈推荐的 IntelliJ IDEA 插件,别说我没告诉你
为什么你的 Intellij IDEA 没别人的好用?还不是因为你缺少这几个插件啊! 善用 Intellij IDEA 插件可以提高我们的开发效率,今天和大家一起分享一下实际工作中常用的几款能提升幸福 ...
- 【CF115E】Linear Kingdom Races 题解(线段树优化DP)
前言:前辈讲课时设的状态还是有些繁琐,感觉题解设的状态更简洁. -------------- 题目链接 题目大意:给定$n$条道路和$m$场比赛,每个道路修建需要$c_i$,每场比赛需要使用$[l_i ...
- 为写程序而生的连字字体 Fira Code
Fira Code,等宽的编程连字字体 **等宽 ** 是指所有字符的宽度相同,如: W 和 i 用一样的宽度去显示 连字(ligatures)为文字排印的一个特性,比如「f」和「 i」放在一起的时候 ...
- 017_go语言中的指针
代码演示 package main import "fmt" func zeroval(ival int) { ival = 0 } func zeroptr(iptr *int) ...
- 《RabbitMQ》如何保证消息不被重复消费
一 重复消息 为什么会出现消息重复?消息重复的原因有两个:1.生产时消息重复,2.消费时消息重复. 1.1 生产时消息重复 由于生产者发送消息给MQ,在MQ确认的时候出现了网络波动,生产者没有收到确认 ...