Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a typedef enum in my object Class

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 enum for this since I have seen it done before and it seems like the cleanest solution. But, I am unsure how to declare this, then make it into a property.

.h

typedef enum {
    kPersonTypeFaculty,
    kPersonTypeStaff,
    kPersonTypeSearch
} personType;

@interface Person : NSObject {
    NSString *nameFirst;
    NSString *nameLast;
    NSString *email;
    NSString *phone;
    NSString *room;
    NSString *status;
    NSString *building;
    NSString *department;
    NSString *imageURL;
    NSString *degree;
    NSString *position;
    NSString *bio;
    NSString *education;
}

@property (nonatomic, retain) NSString *nameFirst;
@property (nonatomic, retain) NSString *nameLast;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *phone;
@property (nonatomic, retain) NSString *room;
@property (nonatomic, retain) NSString *status;
@property (nonatomic, retain) NSString *building;
@property (nonatomic, retain) NSString *department;
@property (nonatomic, retain) NSString *imageURL;
@property (nonatomic, retain) NSString *degree;
@property (nonatomic, retain) NSString *position;
@property (nonatomic, retain) NSString *bio;
@property (nonatomic, retain) NSString *education;

@end

.m

#import "Person.h"


@implementation Person

@synthesize nameFirst, nameLast, email, phone, room, status, building, department, imageURL, degree, position, bio, education;

- (void)dealloc {
    [nameFirst release];
    [nameLast release];
    [email release];
    [phone release];
    [room release];
    [status release];
    [building release];
    [department release];
    [imageURL release];
    [degree release];
    [position release];
    [bio release];
    [education release];
    [super dealloc];
}

@end

I want to be able to do something like:

Person *person = [[[Person alloc] init] autorelease];
person.nameFirst = @"Steve";
person.nameLast = @"Johnson";
person.personType = kPersonTypeStaff;

Am I missing a crucial part of this idea?

like image 490
Nic Hubbard Avatar asked Aug 19 '11 19:08

Nic Hubbard


People also ask

What is typedef enum in Objective C?

A typedef in Objective-C is exactly the same as a typedef in C. And an enum in Objective-C is exactly the same as an enum in C. This declares an enum with three constants kCircle = 0, kRectangle = 1 and kOblateSpheroid = 2, and gives the enum type the name ShapeType.

How do you define an enumeration inside a class and use it from the outside?

The simplest change needed to get your code to work is this: class mySprite : public sf::Sprite { public: enum side{ left=1, right=2, top=3, bottom=4 }; float GetSide(side aSide) { switch(aSide) { // do stuff // add breaks; as appropriate case left: case right: case top: case bottom: } } };


1 Answers

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.

like image 166
ughoavgfhw Avatar answered Oct 17 '22 04:10

ughoavgfhw