0

我有一个关于类方法的问题。首先是我的代码:

typedef enum Colors {
    PresetColorsWhite=0,
    PresetColorsBlue=1,
    PresetColorsLightGray=2,
    PresetColorsGray=3,
    PresetColorsRed=4,
    PresetColorsGreen=5,
    PresetColorsYellow=6,
    PresetColorsOrange=7,
    PresetColorsPurple=8,
    PresetColorsBrown=9,
} PresetColors;

    @interface PresetColor : NSObject

    @property UIColor *color;
    @property NSString *colorName;

    -(void)initWithPresetColor:(PresetColors)presetColor;
    -(NSArray *)getAllColorNames;
    -(UIColor *)getColorWithPresetColor:(PresetColors)presetColor;

和实施:

@interface PresetColor()

@property (nonatomic, strong) NSMutableArray *colorArray;
@property (nonatomic, strong) NSMutableArray *colorNamesArray;

@end

@implementation PresetColor

@synthesize color = _color;
@synthesize colorName = _colorName;

-(id)init{
    [self initWithPresetColor:PresetColorsWhite];
    return self;
}

-(void)initWithPresetColor:(PresetColors)presetColor{
    [self setupDictionaries];

    self.color = [self.colorArray objectAtIndex:presetColor];
    self.colorName = [self.colorNamesArray objectAtIndex:presetColor];
}

- (void)setupDictionaries {
    self.colorArray = [[NSMutableArray alloc]init];
    [self.colorArray insertObject:[UIColor whiteColor] atIndex:PresetColorsWhite];
    [self.colorArray insertObject:[UIColor blueColor] atIndex:PresetColorsBlue];
    [self.colorArray insertObject:[UIColor lightGrayColor] atIndex:PresetColorsLightGray];
    [self.colorArray insertObject:[UIColor grayColor] atIndex:PresetColorsGray];
    [self.colorArray insertObject:[UIColor redColor] atIndex:PresetColorsRed];
    [self.colorArray insertObject:[UIColor greenColor] atIndex:PresetColorsGreen];
    [self.colorArray insertObject:[UIColor yellowColor] atIndex:PresetColorsYellow];
    [self.colorArray insertObject:[UIColor orangeColor] atIndex:PresetColorsOrange];
    [self.colorArray insertObject:[UIColor purpleColor] atIndex:PresetColorsPurple];
    [self.colorArray insertObject:[UIColor brownColor] atIndex:PresetColorsBrown];

    self.colorNamesArray = [[NSMutableArray alloc]init];
    [self.colorNamesArray insertObject:NSLocalizedString(@"White", nil) atIndex:PresetColorsWhite];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Blue", nil) atIndex:PresetColorsBlue];
    [self.colorNamesArray insertObject:NSLocalizedString(@"LightGray", nil) atIndex:PresetColorsLightGray];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Gray", nil) atIndex:PresetColorsGray];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Red", nil) atIndex:PresetColorsRed];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Green", nil) atIndex:PresetColorsGreen];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Yellow", nil) atIndex:PresetColorsYellow];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Orange", nil) atIndex:PresetColorsOrange];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Purple", nil) atIndex:PresetColorsPurple];
    [self.colorNamesArray insertObject:NSLocalizedString(@"Brown", nil) atIndex:PresetColorsBrown];
}

- (NSArray *)getAllColorNames {
    return [self.colorNamesArray copy];
}

- (UIColor *)getColorWithPresetColor:(PresetColors)presetColor{
    return [self.colorArray objectAtIndex:presetColor];
}

我希望在我的应用程序中拥有用户可以从一些预定义的颜色中进行选择的可能性。在数据库中,只应保存 typedef 中的整数。现在我正在寻找最简单的方法来将颜色和颜色描述保存在数组中,并将相应typedef的 'dPresetColors作为键。有些方法当然应该是针对对象的,但有些应该是类方法。我的意思是方法 -(UIColor *)getColorWithPresetColor:(PresetColors)presetColor;实际上应该是一个类方法等。但是如果不每次都初始化数组,我怎么能做到这一点?当然我可以用它做一个复杂的 switch 语句,但是我想知道数组是否有另一种方法,所以我只有一个地方可以设置对象的键,而没有其他地方?我希望我的意思很清楚:)

4

1 回答 1

5

如果我是你,我会将 colorArray 更改为静态实例,例如:

static NSArray *colorArray = nil;

+(NSArray *) colorArray
{
    if (colorArray == nil)
    {
        colorArray = @[[UIColor whiteColor], ...];
    }

    return colorArray;
}

然后;

//This will work because you set enum values in the range 0 - 9 and btw those are the default values you don't need to set them explicitly
+(UIColor *)getColorWithPresetColor:(PresetColors)presetColor
{
    return colorArray[presetColor];
}
于 2013-05-02T09:24:56.117 回答