Our code example demonstrates the use of enumerated types in a program. Here’s how it works in Objective-C.
To run the program:
Save the code in a file named enums.m.
Compile the program using clang or gcc.
The NS_ENUM macro defines the ServerState enum type with underlying NSInteger values. We also define a helper function ServerStateString to convert enum values to readable strings.
The transition function replicates state transitions for a server. It takes the current state and returns a new state based on predefined rules. If the state is unknown, it raises an exception.
Now that we’ve seen how to implement and use enums in Objective-C, we can look at more advanced topics in the language.