How to create NS_OPTIONS-style bitmask enumerations in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, the modern replacement for Objective-C NS_OPTIONS is OptionSet. It gives you the same bitmask-style behavior while keeping the code type-safe and readable, so you can represent combinations of flags without falling back to raw integer constants.
Define an OptionSet
Here is the standard pattern:
Each option gets one bit. That is exactly the same mental model as NS_OPTIONS.
Use Multiple Flags Together
Once defined, options can be combined naturally:
This is much clearer than manually checking integer bitmasks everywhere.
Why OptionSet Is Better Than a Plain Enum
A normal Swift enum represents one case at a time. Bitmask options are different because you often need several values simultaneously.
For example:
- '
showTitle' - '
showIcon'
can both be active together. That is why OptionSet is the right abstraction and a plain enum is not.
Raw Value Type Choices
You can back an OptionSet with different integer types depending on your needs:
For most app code, Int is fine. If you are bridging with C or matching a specific storage layout, choosing an explicit unsigned integer type can make the intent clearer.
A Bridging Example
If you are replacing old Objective-C code like:
the Swift OptionSet version is the intended equivalent.
Add Convenience Groups
You can define grouped options too:
That is useful when a group of flags appears repeatedly in your code.
You also get set-like operations for free, such as union and intersection, which makes option manipulation feel much more natural than hand-writing bitwise expressions everywhere.
Common Operations You Will Actually Use
OptionSet is nice because normal code reads like intent rather than math:
Those APIs make maintenance easier than raw & and | operators, especially once multiple developers touch the codebase.
Interoperability Notes
If you are calling Apple APIs or Objective-C code that historically used NS_OPTIONS, Swift will usually expose those values as an OptionSet already. Matching that style in your own APIs keeps your code consistent with the rest of the platform and makes call sites feel familiar to Swift developers.
Common Pitfalls
- Using a regular enum when the requirement is actually a combinable set of flags.
- Reusing the same bit for more than one option.
- Falling back to raw integer checks everywhere instead of using
contains. - Forgetting that
OptionSetvalues can represent multiple active options at once.
Summary
- Swift uses
OptionSetas theNS_OPTIONS-style bitmask pattern. - Each option should have a unique bit in
rawValue. - '
OptionSetallows clean combination and membership checks.' - It is the correct Swift abstraction for flag-style options.
- Use it whenever multiple enum-like values must be active simultaneously.
Related reading
- How to create NS_OPTIONS-style bitmask enumerations in Swift?
- How to create NSIndexPath for TableView
- How to create P12 certificate for iOS distribution
- How to create percentage of total width using autolayout?
- How to create radio buttons and checkbox in swift iOS?
- How to create range in Swift?
- How to create RecyclerView with multiple view types
- How to create RecyclerView with multiple view types
.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.