1

Please help me construct a multidimensional lookup table, I want to get rid of the damned arrowhead anti pattern and not use ifs and switches at all.

I have two UIControlStates

UIControlStateNormal UIControlStateHighlighted

two sides defined as enums

EnumSideLeft
EnumSideRight

and two shades as enum

EnumShadeLight EnumShadeDark

This is a 2 x 2 x 2 cube. For each cell/combination of three, I have a unique picture.

I want to have a class method that traverses the configuration dictionary and returns a UIimage + a class method that provides the configuration dictionary itself.

But I somehow cannot figure out an effective approach to define that dictionary using modern literals approach + the hierarchy of that cube using dictionary.

Earl Grey
  • 7,426
  • 6
  • 39
  • 59

2 Answers2

4

You might use a NSDictionary with a special key:

NSDictionary* images = ...;

int x,y,z = ...;
NSString* key = [NSString stringWithFormat:@"%d,%d,%d", x,y,z);

UIImage* image = images[key];

That's just one idea. You might construct your key also as a integer with shift and bitwise OR operations, e.g.:

int key = (z<<4) | (y<<2) | x; 

And then create a NSNumber for the key. This is faster than generating a NSString.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
1

I would probably take the approach that CouchDeveloper suggests, but in case you want something specifically along the lines you describe in your question, try something like the following:

static NSDictionary *configDict;

typedef NS_ENUM(NSUInteger, BDSide) {
    BDSideLeft,
    BDSideRight
};


typedef NS_ENUM(NSUInteger, BDShade) {
    BDShadeLight,
    BDShadeDark
};


+ (UIImage *)configurationForState:(UIControlState)state Side:(BDSide)side Shade:(BDShade)shade
{
    UIImage *result = nil;

    NSDictionary *stateDictionary = [configDict objectForKey:@(state)];
    NSDictionary *sideDictionary = [stateDictionary objectForKey:@(side)];

    result = [sideDictionary objectForKey:@(shade)];
    return result;
}


+(void)initialize
{
  configDict = @{@(UIControlStateNormal):
                    @{@(BDSideLeft):
                       @{@(BDShadeLight): [UIImage imageNamed:@"normal-left-light.png"],
                         @(BDShadeDark): [UIImage imageNamed:@"normal-left-dark.png"]},
                      @(BDSideRight):
                         @{@(BDShadeLight): [UIImage imageNamed:@"normal-right-light.png"],
                           @(BDShadeDark): [UIImage imageNamed:@"normal-right-dark.png"]}},
               @(UIControlStateHighlighted):
                    @{@(BDSideLeft):
                         @{@(BDShadeLight): [UIImage imageNamed:@"highlight-left-light.png"],
                           @(BDShadeDark): [UIImage imageNamed:@"highlight-left-dark.png"]},
                    @(BDSideRight):
                         @{@(BDShadeLight): [UIImage imageNamed:@"highlight-right-light.png"],
                           @(BDShadeDark): [UIImage imageNamed:@"highlight-right-dark.png"]}}};
}
Matthew Burke
  • 2,295
  • 17
  • 14