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?
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.
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: } } };
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With