Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant NSDictionary/NSArray for class methods

I am trying to code a global lookup table of sorts.

I have game data that is stored in character/string format in a plist, but which needs to be in integer/id format when it is loaded.

For instance, in the level data file, a "p" means player. In the game code a player is represented as the integer 1. This let's me do some bitwise operations, etc. I am simplifying greatly here, but trying to get the point across. Also, there is a conversion to coordinates for the sprite on a sprite sheet.

Right now this string->integer, integer->string, integer->coordinate, etc. conversion is taking place in several places in code using a case statement. This stinks, of course, and I would rather do it with a dictionary lookup.

I created a class called levelInfo, and want to define the dictionary for this conversion, and then class methods to call when I need to do a conversion, or otherwise deal with level data.

NSString *levelObjects = @"empty,player,object,thing,doohickey";
int levelIDs[] = [0,1,2,4,8];
// etc etc

@implementation LevelInfo

+(int) crateIDfromChar: (char) crateChar {
    int idx = [[crateTypes componentsSeparatedByString:@","] indexOfObject: crateChar];
    return levelIDs[idx];
}

+(NSString *) crateStringFromID: (int) crateID {
    return [[crateTypes componentsSeparatedByString:@","] objectAtIndex: crateID];
}

@end

Is there a better way to do this? It feels wrong to basically build these temporary arrays, or dictionaries, or whatever for each call to do this translation. And I don't know of a way to declare a constant NSArray or NSDictionary.

Please, tell me a better way....

like image 306
Jeff B Avatar asked Dec 06 '22 03:12

Jeff B


1 Answers

If you want an array to be available to all the code in your class, just declare it outside the @implementation context, and then initialize it in your class's +initialize method.

NSArray *levelObjects;

@implementation LevelInfo

+ (void) initialize
 {
 if (!levelObjects)
   levelObjects = [[NSArray alloc]
    initWithObjects:@"empty",@"player",@"object",@"thing",@"doohickey",nil];
 }

// now any other code in this file can use "levelObjects"

@end
like image 61
NSResponder Avatar answered Jan 11 '23 19:01

NSResponder