在我的对象Class中使用typedef枚举 [英] Using a typedef enum in my object Class

查看:108
本文介绍了在我的对象Class中使用typedef枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个People类,它包含了关于一个人的各种内容。我希望能够确定这是什么类型的人,所以我想我会尝试使用typedef枚举,因为我之前已经看过它,它似乎是最干净的解决方案。但是,我不确定如何申报,然后将其变成财产。

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?

推荐答案

你可以像任何原始类型一样定义它(比如 int float )。当您使用 typedef 时,您告诉编译器此名称是表示此名称的类型。所以,你要添加一个具有该类型的实例变量(我将我的帖子中的类型大写,以区别于变量或属性):

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;

然后添加属性定义:

@property (nonatomic) personType personType;

然后你合成并使用它:

@synthesize personType;

self.personType = kPersonTypeStaff;

枚举类型实际上是某种类型的整数,它包含枚举的所有值。通过使用 typedef ,您可以指定此整数应该是枚举中的常量之一,而不是其他任何内容,编译器可以帮助强制执行此操作。但是,除了变量类型之外,您对它的处理方式与 int 类型完全相同。

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.

这篇关于在我的对象Class中使用typedef枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆