Using a typedef enum in my object Class

前端 未结 2 1803
别跟我提以往
别跟我提以往 2021-02-19 22:02

I have a People class which holds various bits of into about a person. I would like to be able to identify what kind of person this is, so I thought I would try using a typedef

相关标签:
2条回答
  • 2021-02-19 22:16

    You define it like you would for any primitive type (like int or float). When you use typedef, you are telling the compiler that this name is a type which represents this. So, you would add an instance variable with that type (I capitalized the type in my post to distinguish it from the variable or property):

    personType personType;
    

    Then add a property definition:

    @property (nonatomic) personType personType;
    

    Then you synthesize it and use it:

    @synthesize personType;
    
    self.personType = kPersonTypeStaff;
    

    A enum type is actually some type of integer which holds all of the values of the enum. By using typedef, you can specify that this integer should be one of the constants in the enum and nothing else, and the compiler can help enforce this. But, except for the variable type, you treat it exactly the same way you would an int type.

    0 讨论(0)
  • 2021-02-19 22:28

    You add the following property:

    @property (nonatomic) personType personType;
    
    0 讨论(0)
提交回复
热议问题