Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 41 / 68

The basics of enums

This lesson covers the basics of NS_ENUM in Objective-C, with the aim of handling a fixed set of options under readable names.

The NS_ENUM macro creates a type-safe enumerated type representing a fixed set of options. Use it when you want to limit the possible values in advance — a traffic light is only ever red, green, or amber.

The sample code defines three options with typedef NS_ENUM(NSInteger, Color) { ColorRed, ColorGreen, ColorBlue }; and assigns one to a variable as Color c = ColorRed;.

A common early stumble is the difference from C's older enum syntax. Using the NS_ENUM macro gives you a more type-safe enum that the compiler can warn about.

In professional work, a named enum conveys intent far better than scattering strings or numbers through the code.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)