Swift - Cast Int into enumInt
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Because this enum has Int raw values, Swift automatically gives you init?(rawValue:).
Convert an Int to the Enum
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:
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:
This keeps the fallback decision centralized instead of repeating ?? everywhere.
Converting Back to Int
The reverse direction is easy:
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.
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:
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:
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 anIntto 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
.rawValueto convert the enum back toInt. - Do not use
asfor raw-value enum conversion.
Related reading
- Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago
- Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago
- Swift - class method which must be overridden by subclass
- Swift - Convert to absolute value
- Swift - Convert to absolute value
- Swift - encode URL
- Swift - encode URL
- Swift - How creating custom viewForHeaderInSection, Using a XIB file?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.