protocol
RawRepresentable
Inheritance | View Protocol Hierarchy → |
---|---|
Associated Types |
The raw type that can be used to represent all values of the conforming type. Every distinct value of the conforming type has a corresponding unique
value of the |
Import |
|
Initializers
Creates a new instance with the specified raw value.
If there is no value of the type that corresponds with the specified raw
value, this initializer returns nil
. For example:
enum
PaperSize
:
String
{
case
A4
,
A5
,
Letter
,
Legal
}
(
PaperSize
(
rawValue
:
"Legal"
))
// Prints "Optional("PaperSize.Legal")"
(
PaperSize
(
rawValue
:
"Tabloid"
))
// Prints "nil"
rawValue
: The raw value to use for the new instance.
Declaration
init
?(
rawValue
:
Self
.
RawValue
)
Instance Variables
The corresponding value of the raw type.
A new instance initialized with rawValue
will be equivalent to this
instance. For example:
enum
PaperSize
:
String
{
case
A4
,
A5
,
Letter
,
Legal
}
let
selectedSize
=
PaperSize
.
Letter
(
selectedSize
.
rawValue
)
// Prints "Letter"
(
selectedSize
==
PaperSize
(
rawValue
:
selectedSize
.
rawValue
)!)
// Prints "true"
Declaration
var
rawValue
:
Self
.
RawValue
{
get
}
Default Implementations
Where RawValue == Bool
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Bool
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Bool
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Double
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Double
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Double
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Float
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Float
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Float
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Int
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Int
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Int
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Int16
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Int16
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Int16
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Int32
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Int32
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Int32
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Int64
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Int64
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Int64
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == Int8
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is Int8
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is Int8
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == String
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is String
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is String
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == UInt
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is UInt
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is UInt
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == UInt16
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is UInt16
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is UInt16
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == UInt32
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is UInt32
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is UInt32
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == UInt64
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is UInt64
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is UInt64
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
Where RawValue == UInt8
Creates a new instance by decoding from the given decoder, when the
type's RawValue
is UInt8
.
This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.
decoder
: The decoder to read data from.
Declaration
init
(
from
decoder
:
Decoder
)
Encodes this value into the given encoder, when the type's RawValue
is UInt8
.
This function throws an error if any values are invalid for the given encoder's format.
encoder
: The encoder to write data to.
Declaration
func
encode
(
to
encoder
:
Encoder
)
throws
A type that can be converted to and from an associated raw value.
With a
RawRepresentable
type, you can switch back and forth between a custom type and an associatedRawValue
type without losing the value of the originalRawRepresentable
type. Using the raw value of a conforming type streamlines interoperation with Objective-C and legacy APIs and simplifies conformance to other protocols, such asEquatable
,Comparable
, andHashable
.The
RawRepresentable
protocol is seen mainly in two categories of types: enumerations with raw value types and option sets.Enumerations with Raw Values
For any enumeration with a string, integer, or floating-point raw type, the Swift compiler automatically adds
RawRepresentable
conformance. When defining your own custom enumeration, you give it a raw type by specifying the raw type as the first item in the enumeration's type inheritance list. You can also use literals to specify values for one or more cases.For example, the
Counter
enumeration defined here has anInt
raw value type and gives the first case a raw value of1
:You can create a
Counter
instance from an integer value between 1 and 5 by using theinit?(rawValue:)
initializer declared in theRawRepresentable
protocol. This initializer is failable because although every case of theCounter
type has a correspondingInt
value, there are manyInt
values that don't correspond to a case ofCounter
.Option Sets
Option sets all conform to
RawRepresentable
by inheritance using theOptionSet
protocol. Whether using an option set or creating your own, you use the raw value of an option set instance to store the instance's bitfield. The raw value must therefore be of a type that conforms to theFixedWidthInteger
protocol, such asUInt8
orInt
. For example, theDirection
type defines an option set for the four directions you can move in a game.Unlike enumerations, option sets provide a nonfailable
init(rawValue:)
initializer to convert from a raw value, because option sets don't have an enumerated list of all possible cases. Option set values have a one-to-one correspondence with their associated raw values.In the case of the
Directions
option set, an instance can contain zero, one, or more of the four defined directions. This example declares a constant with three currently allowed moves. The raw value of theallowedMoves
instance is the result of the bitwise OR of its three members' raw values:Option sets use bitwise operations on their associated raw values to implement their mathematical set operations. For example, the
contains()
method onallowedMoves
performs a bitwise AND operation to check whether the option set contains an element.