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];
开发者_如何学Go[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?
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.
You add the following property:
@property (nonatomic) personType personType;
精彩评论