It will take about 3 minutes to finish reading this article.
1. NS_OPTIONS and NS_ENUM in OC
The following enumeration definitions are common in Objective-C:
1 |
|
1 |
|
Their differences:
NS_ENUM: used to define a set of related discrete values that cannot be combined by bit operations.
NS_OPTIONS: Used to define a set of discrete values that can be combined by bit operations, usually used to represent a set of options or flags.
Here is an example of using NS_OPTIONS:
1 |
|
The calling code is as follows:
1 |
|
Results of the:
1 | Option 1 is selected. |
2. OptionSet protocol
OptionSet is the Swift version of NS_OPTIONS, but it is not a simple replacement, it is more complex.
In Swift, an OptionSet is a protocol used to represent a set of options or a collection of flags. It is a type-safe way to manage bit masks with discrete values. The OptionSet protocol allows you to define a set of options with a bit mask and operate in a set-like manner. Some types in the Swift standard library, such as NSOptions, UIControlEvents, etc., implement the OptionSet protocol.
1 | public protocol OptionSet: RawRepresentable, SetAlgebra { |
(1) The OptionSet protocol declares a public access level, indicating that it is publicly visible.
(2) The OptionSet protocol inherits two protocols: RawRepresentable and SetAlgebra. This means that any type that conforms to the OptionSet protocol must satisfy the requirements of both protocols.
(3) associatedtype Element = Self: This section specifies the associated type Element in the OptionSet protocol and sets it to Self by default, which means that the type of Element will be the type itself that follows the OptionSet protocol.
(4) init(rawValue: Self.RawValue): This is a declaration of an initialization method that allows the creation of an OptionSet instance from a raw value. This method must be implemented.
Next is an extension to the OptionSet protocol, which provides some default implementations:
1 | extension OptionSet { |
These extensions provide default set operation methods for types that follow the OptionSet protocol, including union, intersection, and symmetric difference. These methods are useful when combining options using bit masks, and they allow you to easily perform various operations such as adding, removing, or checking options.
3. Sample code
Let’s take a simple example to understand OptionSet in depth.
(1) First, we define a theme option set that follows the OptionSet protocol.
1 | struct ThemeOptions: OptionSet { |
We create a structure called ThemeOptions, which conforms to the OptionSet protocol. We define four theme options: dark, light, blue, and green, and assign a bitmask value to each option.
(2) Users choose different themes:
1 | var selectedOptions: ThemeOptions = [.dark, .blue] |
(3) Use the union method to add more options:
1 | selectedOptions = selectedOptions.union(.green) |
(4) To check whether an option has been selected, you can use the contains method:
1 | if selectedOptions.contains(.light) { |