Can I use an enum as a property in Objective C

21,539

Solution 1

Yes, it's not a problem:

@property (nonatomic, assign) stackState yourIvar;

Solution 2

@property (nonatomic, assign) enum stackState stackStateVar;

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

Solution 3

@property (nonatomic, assign) enum stackState yourIvar;

(was getting errors until I added enum)

Share:
21,539
bursyllac
Author by

bursyllac

Updated on July 09, 2022

Comments

  • bursyllac
    bursyllac almost 2 years

    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;
    
  • Ousmane D.
    Ousmane D. about 7 years
    this post has been here for 4 years, i don't think any new answers would get much attention.
  • Philipp Hofmann
    Philipp Hofmann over 3 years
    Could be because you don't use typedef or NS_ENUM for your enum.