我对课程方法有疑问。首先是我的代码:
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
'd PresetColors
作为关键字。有些方法当然应该是对象,但有些方法应该是类方法。
我的意思是方法 - (UIColor *)getColorWithPresetColor:(PresetColors)presetColor;
实际上应该是一个类方法等。但是如果没有每次初始化数组我怎么能这样做呢?当然我可以用它来制作一个复杂的switch语句,但是我想知道是否有另一种方式使用数组,所以我只有一个地方可以设置对象的键而不是其他地方?我希望我的意思很明确:))
答案 0 :(得分: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];
}