NSOPTIONS
NS_OPTIONS is a macro used in Objective-C to declare bitmask-based option sets. It defines a new, strongly-typed unsigned integer type whose values are intended to be combined with bitwise operations. The macro helps create a collection of independent flags that can be stored in a single variable and tested efficiently.
Typically, NS_OPTIONS is used like this:
typedef NS_OPTIONS(NSUInteger, MyOptions) {
MyOptionFirst = 1 << 0,
MyOptionSecond = 1 << 1,
MyOptionThird = 1 << 2
};
A variable of type MyOptions can hold any combination of these flags. For example:
MyOptions options = MyOptionFirst | MyOptionSecond;
if (options & MyOptionFirst) {
}
Checking for a specific flag is usually done with a bitwise AND operation.
Swift interoperability treats NS_OPTIONS as a bridged type that maps to the Swift OptionSet protocol, enabling
NS_OPTIONS is contrasted with NS_ENUM, which defines a plain enumeration where only one value is expected
Originating in Apple’s Cocoa and Cocoa Touch frameworks, NS_OPTIONS is commonly used to configure behavior or