Using a typedef enum in my object Class

前端 未结 2 1790
别跟我提以往
别跟我提以往 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题