2011-08-19 58 views
12

Tôi có một lớp People chứa nhiều bit khác nhau về một người. Tôi muốn có thể xác định loại người này là gì, vì vậy tôi nghĩ rằng tôi sẽ thử sử dụng một typedef enum cho điều này kể từ khi tôi đã nhìn thấy nó được thực hiện trước và nó có vẻ như là giải pháp sạch nhất. Nhưng, tôi không chắc chắn làm thế nào để tuyên bố điều này, sau đó làm cho nó thành một tài sản.Dùng typedef enum trong đối tượng của tôi Lớp

.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 

Tôi muốn để có thể làm điều gì đó như:

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

Tôi có thiếu một phần quan trọng của ý tưởng này?

Trả lời

17

Bạn định nghĩa nó như bạn sẽ cho bất kỳ loại nguyên thủy (như int hay float). Khi bạn sử dụng typedef, bạn đang nói với trình biên dịch rằng tên này là một loại đại diện cho điều này. Vì vậy, bạn sẽ thêm một biến Ví dụ với loại đó (tôi vốn hóa các loại trong bài của tôi để phân biệt nó với các biến hoặc tài sản):

personType personType; 

Sau đó thêm một định nghĩa bất động sản:

@property (nonatomic) personType personType; 

Sau đó, bạn tổng hợp và sử dụng nó:

@synthesize personType; 

self.personType = kPersonTypeStaff; 

Loại enum thực ra là một số nguyên chứa tất cả các giá trị của enum. Bằng cách sử dụng typedef, bạn có thể chỉ định rằng số nguyên này phải là một trong các hằng số trong enum và không có gì khác, và trình biên dịch có thể giúp thực thi điều này. Tuy nhiên, ngoại trừ loại biến, bạn đối xử với nó chính xác giống như cách bạn sẽ là loại int.

+0

Awe, thật dễ dàng! Cảm ơn bạn! –

2

Bạn thêm các tài sản sau đây:

@property (nonatomic) personType personType; 
+1

Bạn không thể giữ lại một điều tra. – ughoavgfhw

+0

Cảm ơn, sao chép và dán lỗi! –

Các vấn đề liên quan