RawRepresentable
声明
protocol RawRepresentable
概述
使用RawRepresentable
类型,您可以在自定义类型和关联的RawValue
类型之间来回切换,而不会丢失原始RawRepresentable
类型的值。使用符合类型的原始值简化了与 Objective-C 和旧版 API 的互操作,并简化了与其他协议的一致性,例如 Equatable
、 Comparable
和 Hashable
。
RawRepresentable
协议主要有两类类型:具有原始值类型的枚举和选项集。
具有原始值的枚举
对于任何具有字符串、整数或浮点原始类型的枚举,Swift 编译器会自动添加RawRepresentable
一致性。在定义您自己的自定义枚举时,您可以通过将原始类型指定为枚举类型继承列表中的第一项来为其提供原始类型。您还可以使用文字来指定一种或多种情况的值。
例如,此处定义的 Counter
枚举具有 Int
原始值类型,并为第一种情况提供 1
的原始值:
enum Counter: Int {
case one = 1, two, three, four, five
}
您可以使用 RawRepresentable
协议中声明的 init?(rawValue:)
初始化程序从 1 到 5 之间的整数值创建 Counter
实例。此初始化程序是可失败的,因为尽管 Counter
类型的每个案例都有一个对应的 Int
值,但有许多 Int
值 don’t
对应于 Counter
的案例。
for i in 3...6 {
print(Counter(rawValue: i))
}
// Prints "Optional(Counter.three)"
// Prints "Optional(Counter.four)"
// Prints "Optional(Counter.five)"
// Prints "nil"
选项集
通过使用OptionSet
协议继承,选项集全部符合RawRepresentable
。无论是使用选项集还是创建自己的选项集,您都可以使用选项集实例的原始值来存储实例的位域。因此,原始值必须是符合 FixedWidthInteger
协议的类型,例如 UInt8
或 Int
。例如,Direction
类型为您可以在游戏中移动的四个方向定义了一个选项集。
struct Directions: OptionSet {
let rawValue: UInt8
static let up = Directions(rawValue: 1 << 0)
static let down = Directions(rawValue: 1 << 1)
static let left = Directions(rawValue: 1 << 2)
static let right = Directions(rawValue: 1 << 3)
}
与枚举不同,选项集提供了一个不可失败的 init(rawValue:)
初始化程序来从原始值转换,因为选项集没有所有可能情况的枚举列表。选项集值与其关联的原始值一一对应。
在Directions
选项集的情况下,实例可以包含四个已定义方向中的零个、一个或多个。此示例声明了一个具有三个当前允许移动的常量。 allowedMoves
实例的原始值是其三个成员原始值的按位或结果:
let allowedMoves: Directions = [.up, .down, .left]
print(allowedMoves.rawValue)
// Prints "7"
选项集对其关联的原始值使用按位运算来实现其数学集运算。例如,allowedMoves
上的contains()
方法执行按位与运算来检查选项集是否包含元素。
print(allowedMoves.contains(.right))
// Prints "false"
print(allowedMoves.rawValue & Directions.right.rawValue)
// Prints "0"
可用版本
相关用法
- Swift RawRepresentable rawValue用法及代码示例
- Swift RawRepresentable init(rawValue:)用法及代码示例
- Swift RandomNumberGenerator用法及代码示例
- Swift RangeReplaceableCollection insert(_:at:)用法及代码示例
- Swift Range clamped(to:)用法及代码示例
- Swift RangeReplaceableCollection filter(_:)用法及代码示例
- Swift RangeReplaceableCollection removeFirst(_:)用法及代码示例
- Swift RangeReplaceableCollection用法及代码示例
- Swift RangeReplaceableCollection removeSubrange(_:)用法及代码示例
- Swift RangeReplaceableCollection replaceSubrange(_:with:)用法及代码示例
- Swift RangeReplaceableCollection removeFirst()用法及代码示例
- Swift RangeReplaceableCollection remove(at:)用法及代码示例
- Swift RandomAccessCollection subscript(_:)用法及代码示例
- Swift RangeExpression ~=(_:_:)用法及代码示例
- Swift Range overlaps(_:)用法及代码示例
- Swift RangeReplaceableCollection append(_:)用法及代码示例
- Swift RangeReplaceableCollection +=(_:_:)用法及代码示例
- Swift Range用法及代码示例
- Swift RandomAccessCollection indices用法及代码示例
- Swift Range endIndex用法及代码示例
- Swift RangeReplaceableCollection removeAll(where:)用法及代码示例
- Swift RandomAccessCollection index(_:offsetBy:)用法及代码示例
- Swift Range ~=(_:_:)用法及代码示例
- Swift RangeReplaceableCollection +(_:_:)用法及代码示例
- Swift RangeExpression relative(to:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 RawRepresentable。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。