如何基于typedef枚举基类行为,Apple在Objective-C中的表现方式?

iPh*_*her 0 cocoa enums cocoa-touch objective-c

一种奇怪的新手问题...我想在我的一个类中使用typedef枚举声明.这个特定的类被其他类使用,其中客户端类可能会说"将样式设置为Style1(枚举类型)".然后目标类的行为将相应地改变.这类似于在iPhone SDK中使用UITableViewCellStyles的方式.

因此,我阅读了一些UIKit框架标题,以便更好地了解Apple如何处理枚举类型.我看到他们到处宣布了一堆枚举,就像这样......

typedef enum {
    UIBarButtonSystemItemDone,
    UIBarButtonSystemItemCancel,
    UIBarButtonSystemItemEdit,  
    UIBarButtonSystemItemSave,  
    UIBarButtonSystemItemAdd,
    ...
    UIBarButtonSystemItemUndo,      // available in iPhone 3.0
    UIBarButtonSystemItemRedo,      // available in iPhone 3.0
} UIBarButtonSystemItem;
Run Code Online (Sandbox Code Playgroud)

...但我没有在标题中看到关于它们如何实际处理这些类型的任何线索(我基本上试图查看它们的实现示例,所以这不是一个惊喜).我本能的想法是一个相当新的程序员将匹配每种类型的int值与存储在数组,plist等中的某些行为/变量.但作为一个新手程序员,我希望我认为的一切都是错误的.所以我有两个问题:

  1. 有人猜测Apple本身如何处理枚举类型值以改变行为?
  2. 一般来说,对于这种类型的设置,是否存在每个人都知道的最佳设计实践,或者这只是一个开放式场景?

Dar*_*ren 8

简单的枚举通常在switch语句中处理:

typedef enum {
    eRedStyle,
    eGreenStyle,
    eBlueStyle
} MyStyle;

@implementation MyClass

- (void)setStyle:(MyStyle)style {
    switch (style) {
        case eRedStyle :
            self.backgroundColor = [UIColor redColor];
            break;
        case eGreenStyle :
            self.backgroundColor = [UIColor greenColor];
            break;
        case eBlueStyle :
            self.backgroundColor = [UIColor blueColor];
            break;
        default :
            NSLog(@"Bad Style: %d", style);
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)