Swift
Enum
Type Casting
Integer Conversion
Programming

Swift - Cast Int into enumInt

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, you do not cast an Int to an enum with as when the enum has integer raw values. Instead, you use the enum’s failable init?(rawValue:), which safely returns an optional because not every integer necessarily matches a valid case.

Define an Enum with Int Raw Values

swift
1enum SyncState: Int {
2    case idle = 0
3    case running = 1
4    case failed = 2
5    case completed = 3
6}

Because this enum has Int raw values, Swift automatically gives you init?(rawValue:).

Convert an Int to the Enum

swift
1let code = 2
2
3if let state = SyncState(rawValue: code) {
4    print(state)
5} else {
6    print("Unknown code")
7}

This is the normal and safe conversion path.

If code is 2, the result is .failed. If the value does not match any case, the initializer returns nil.

Why This Is Failable

Consider:

swift
let code = 99
let state = SyncState(rawValue: code)
print(state as Any)

There is no valid SyncState with raw value 99, so Swift cannot create one. That is why the initializer is optional instead of forcing a value that would be meaningless.

Providing a Fallback

If your application wants a default for unknown raw values, add a helper:

swift
1enum DownloadStatus: Int {
2    case pending = 0
3    case active = 1
4    case done = 2
5
6    static func from(_ raw: Int) -> DownloadStatus {
7        return DownloadStatus(rawValue: raw) ?? .pending
8    }
9}
10
11print(DownloadStatus.from(7))

This keeps the fallback decision centralized instead of repeating ?? everywhere.

Converting Back to Int

The reverse direction is easy:

swift
let status = DownloadStatus.done
let value = status.rawValue
print(value)   // 2

That is useful for persistence, API payloads, and interoperability with older integer-based systems.

JSON and Storage Boundaries

Raw-value enums are common when decoding server responses or reading stored numeric status codes. The safe pattern is to decode the integer first, then map it through rawValue.

swift
let rawStatus = 1
let status = DownloadStatus(rawValue: rawStatus) ?? .pending

That way unknown future values do not crash the app.

Why as Is the Wrong Mental Model

as and as? are for type casting between runtime-compatible types. Raw-value enum conversion is not a cast. You are asking the enum to look up whether a raw integer corresponds to one of its declared cases. That is why the API is EnumType(rawValue:) instead of a cast operator in Swift.

Using the Enum After Conversion

Once the integer is safely converted, the rest of the code becomes cleaner and more expressive:

swift
1if let state = SyncState(rawValue: code) {
2    switch state {
3    case .idle: print("Idle")
4    case .running: print("Running")
5    case .failed: print("Failed")
6    case .completed: print("Completed")
7    }
8}

Common Pitfalls

One common mistake is trying to use as or as? for this conversion. Raw-value enum lookup is not the same as type casting.

Another issue is force-unwrapping the result:

swift
let state = SyncState(rawValue: 99)!

That will crash for invalid values.

A third pitfall is changing raw values later without thinking about stored data or API compatibility.

Summary

  • Use EnumType(rawValue: someInt) to convert an Int to a raw-value enum in Swift.
  • The initializer is optional because not every integer maps to a valid case.
  • Use ?? or a helper method if you need a fallback value.
  • Use .rawValue to convert the enum back to Int.
  • Do not use as for raw-value enum conversion.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.