Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12376

How do I achieve Enum myEnum=Enum.FirstValue in Objective-C

In C# I can use enums like this:

enum TrafficLight{Red,Yellow,Green};
.......
TrafficLight light=TrafficLight.Yellow;

In Objective-C I write:

    typedef enum trafficLight{
    Red,
    Yellow,
    Green
}TrafficLight; 

But if I want to assign a variable of TrafficLight to TrafficLight.Yellow, it's not possible. I can only write

TrafficLight light=Yellow;

Is it possible to access the constants inside the enum with dot notation in Objective-C too?

Upvotes: 0

Views: 341

Answers (1)

The Lazy Coder
The Lazy Coder

Reputation: 11838

The common practice is as follows

enum {
    TrafficLightColorRed,
    TrafficLightColorYellow,
    TrafficLightColorGreen
};
typedef NSInteger TrafficLightColor;


TrafficLightColor color = TrafficLightColorYellow;

this way the Type of the parameter is the Beginning of its enum elements

Sorry i could not get the result you were looking for but this is the stardard practice as far as I have seen.

Upvotes: 3

Related Questions