jeudi 17 décembre 2015

Good pattern to select multiple values

I have a method that returns an enum value. That enum value is then used to select an image and a color (in two separate functions). I'm trying to figure out a better way to do this than I already have. Currently, I'm using that enum value as an index to a couple of arrays.

This is the method that gives an enum value based on some logic.

-(NSInteger)giveMeAValuePlease {
    if (something)
        return enumValue0;
    else if (somethingElse)
        return enumValue1;
    else
        return enumValue2;
}

This is the method that uses the enum value to select an image

-(void)methodThatRequiresImage {
    NSInteger imageNeeded = [self giveMeAValuePlease];
    UIImage* imageRequired = [self.imagesArray objectAtIndex:imageNeeded];
}

This is the method that uses the enum value to select the color

-(void)methodThatRequiresColor {
    NSInteger colorNeeded = [self giveMeAValuePlease];
    UIColor* colorRequired = [self.colorsArray objectAtIndex:colorNeeded];
}

While this is not a terrible solution, I feel it is prone to errors in the future if values need to be added/removed. I would appreciate any input on what I currently have.

Aucun commentaire:

Enregistrer un commentaire