<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/RawRepresentable",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SY"
  },
  "title" : "RawRepresentable"
}
-->

# RawRepresentable

A type that can be converted to and from an associated raw value.

```
protocol RawRepresentable<RawValue>
```

## Overview

With a `RawRepresentable` type, you can switch back and forth between a
custom type and an associated `RawValue` type without losing the value of
the original `RawRepresentable` type. Using the raw value of a conforming
type streamlines interoperation with Objective-C and legacy APIs and
simplifies conformance to other protocols, such as `Equatable`,
`Comparable`, and `Hashable`.

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 an `Int` raw value
type and gives the first case a raw value of `1`:

```
enum Counter: Int {
    case one = 1, two, three, four, five
}
```

You can create a `Counter` instance from an integer value between 1 and 5
by using the `init?(rawValue:)` initializer declared in the
`RawRepresentable` protocol. This initializer is failable because although
every case of the `Counter` type has a corresponding `Int` value, there
are many `Int` values that *don’t* correspond to a case of `Counter`.

```
for i in 3...6 {
    print(Counter(rawValue: i))
}
// Prints "Optional(Counter.three)"
// Prints "Optional(Counter.four)"
// Prints "Optional(Counter.five)"
// Prints "nil"
```

# Option Sets

Option sets all conform to `RawRepresentable` by inheritance using the
`OptionSet` 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 the
`FixedWidthInteger` protocol, such as `UInt8` or `Int`. For example, the
`Direction` type defines an option set for the four directions you can
move in a game.

```
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)
}
```

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 the
`allowedMoves` instance is the result of the bitwise OR of its three
members’ raw values:

```
let allowedMoves: Directions = [.up, .down, .left]
print(allowedMoves.rawValue)
// Prints "7"
```

Option sets use bitwise operations on their associated raw values to
implement their mathematical set operations. For example, the `contains()`
method on `allowedMoves` performs a bitwise AND operation to check whether
the option set contains an element.

```
print(allowedMoves.contains(.right))
// Prints "false"
print(allowedMoves.rawValue & Directions.right.rawValue)
// Prints "0"
```

## Topics

### Creating a Value

[`init(rawValue:)`](/documentation/Swift/RawRepresentable/init(rawValue:))

Creates a new instance with the specified raw value.

### Accessing the Raw Value

[`rawValue`](/documentation/Swift/RawRepresentable/rawValue-swift.property)

The corresponding value of the raw type.

[`RawValue`](/documentation/Swift/RawRepresentable/RawValue-swift.associatedtype)

The raw type that can be used to represent all values of the conforming
type.

### Comparing Values

[`==(_:_:)`](/documentation/Swift/==(_:_:)-9hu5c)

Returns a Boolean value indicating whether the two arguments are equal.

[`!=(_:_:)`](/documentation/Swift/!=(_:_:)-9wy5n)

Returns a Boolean value indicating whether the two arguments are not equal.

[`!=(_:_:)`](/documentation/Swift/!=(_:_:)-8pggn)

Returns a Boolean value indicating whether the two arguments are not equal.

### Decoding a Value

These initializer overloads are available for any conforming type with a `RawValue`
that is a `Decodable` standard library type.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-5auil)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `String`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-5ar5m)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Bool`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-417i8)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Double`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-9u9tp)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Float`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-4ibll)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Int`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-3hvw1)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `UInt`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-5ktev)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Int8`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-2hvc0)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Int16`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-114vz)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Int32`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-29lhi)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `Int64`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-94955)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `UInt8`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-6z4x4)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `UInt16`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-3arr3)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `UInt32`.

[`init(from:)`](/documentation/Swift/RawRepresentable/init(from:)-812cy)

Creates a new instance by decoding from the given decoder, when the
type’s `RawValue` is `UInt64`.

### Encoding a Value

These overloads are available for any conforming type with a `RawValue` that is an
`Encodable` standard library type.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-4evma)

Encodes this value into the given encoder, when the type’s `RawValue`
is `String`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-5igsi)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Bool`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-4tbh4)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Double`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-21ma8)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Float`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-8horh)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Int`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-78oqu)

Encodes this value into the given encoder, when the type’s `RawValue`
is `UInt`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-4pavm)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Int8`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-86dqn)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Int16`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-7dyeb)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Int32`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-4gohs)

Encodes this value into the given encoder, when the type’s `RawValue`
is `Int64`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-9u5rt)

Encodes this value into the given encoder, when the type’s `RawValue`
is `UInt8`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-cla3)

Encodes this value into the given encoder, when the type’s `RawValue`
is `UInt16`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-27waz)

Encodes this value into the given encoder, when the type’s `RawValue`
is `UInt32`.

[`encode(to:)`](/documentation/Swift/RawRepresentable/encode(to:)-16ame)

Encodes this value into the given encoder, when the type’s `RawValue`
is `UInt64`.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)