Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an enum as a property in Objective C

I saw it is customed to use a boolean property as a flag. something like that:

@property (nonatomic) BOOL commaAlreadyIntroduced; 

I need something like that but with at least 3 or 4 states.

Can I use an enum?

The standalone enum should look like:

typedef enum stackState{     empty, oneOperand, operandAndOperator, fullStack }stackState; 
like image 877
bursyllac Avatar asked May 29 '12 14:05

bursyllac


People also ask

Why don't we use strong for enum property in Objective-C?

We don't use strong for enum property in Objective-C because enums aren't objects so we can 't specify them as strong or weak.

How do I create an enum in Objective-C?

Objective-C Language Enums Defining an enumtypedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, }; You can also specify on the first value and all the following will use it with increment: typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB, MyEnumValueC, };


2 Answers

Yes, it's not a problem:

@property (nonatomic, assign) stackState yourIvar; 
like image 61
Pfitz Avatar answered Oct 13 '22 23:10

Pfitz


@property (nonatomic, assign) enum stackState stackStateVar; 

Without 'enum' added, my unit tests kept showing errors.

like image 37
Arvin Sanmuga Rajah Avatar answered Oct 13 '22 23:10

Arvin Sanmuga Rajah