objective c - NSDictionary with string constants and ids -


i'm looking nice solution problem. need 20+ string constants (resources filenames). i'll need to:

  1. group constants (filenames)
  2. iterate through selected group of constants
  3. access constants id

so best solution found create constant nsdictionary each group constant values , ids. wanted use enum storing constants ids int couldn't used key in nsdictionary.

typedef enum {     ksoundcolorselection1 = 0,     ksoundcolorselection2,     ksoundcolorselection3,     ksoundfill1,     ksoundfill2,     ksoundsizeselectionbig,     ksoundsizeselectionmedium } acsoundid; 

"easiest" way explicitly convert int nsnumber

templatessounds = [[nsdictionary alloc] initwithobjectsandkeys:@"album_close.caf",[nsnumber numberwithint:ksoundalbumclose],                  @"album_open.caf", [nsnumber numberwithint:ksoundalbumopen], nil]; 

is there other way keep constants in case? thanks!

there 2 possible ways make easier.

solution 1 if using acsoundid key nsdictionary, use nsarray:

typedef enum {     ksoundcolorselection1      = 0,     ksoundcolorselection2      = 1,     ksoundcolorselection3      = 2,     ksoundfill1                = 3,     ksoundfill2                = 4,     ksoundsizeselectionbig     = 5,     ksoundsizeselectionmedium  = 6 } acsoundid; 

store sounds in array in same order defined in above enum:

templatessounds = [[nsarray alloc] initwithobjects:@"color_selection1.caf",     @"color_selection2.caf",     @"color_selection3.caf",     @"color_fill1.caf",     @"color_fill2.caf",     @"size_selection_big.caf",     @"size_selection_medium.caf",     nil]; 

since index of sounds correlate values of enum, work similar nsdirectory:

queuedsound = [templatessounds objectatindex:ksoundcolorselection2]; 

solution 2 alternatively create category make easier use integers keys in nsdictionary:

define category:

@interface nsmutabledictionary (nsnumberdictionary) - (void) setobject:(id)anobject fornumber:(nsinteger)anumber; - (void) removeobjectfornumber:(nsinteger)anumber; - (id)   objectfornumber:(nsinteger)anumber; @end 

implement category:

@implementation nsmutabledictionary (nsnumberdictionary) - (void) setobject:(id)anobject fornumber:(nsinteger)anumber {    nsnumber * number;    number = [[nsnumber alloc] initwithinteger:anumber];    [self setobject:anobject forkey:number];    [number release];    return; } - (void) removeobjectfornumber:(nsinteger)anumber {    nsnumber * number;    number = [[nsnumber alloc] initwithinteger:anumber];    [self removeobjectforkey:number];    [number release];    return; } - (id) objectfornumber:(nsinteger)anumber {    nsnumber * number;    id         object;    number = [[nsnumber alloc] initwithinteger:anumber];    object = [self objectforkey:number];    [number release];    return(object); } @end 

use category:

templatessounds = [[nsmutabledictionary alloc] initwithwithcapacity1]; [templatessounds setobject:@"color_selection1.caf" fornumber:ksoundcolorselection1]; [templatessounds setobject:@"color_selection2.caf" fornumber:ksoundcolorselection2]; [templatessounds setobject:@"color_selection3.caf" fornumber:ksoundcolorselection3]; [templatessounds setobject:@"color_fill1.caf"      fornumber:ksoundcolorfill1]; [templatessounds setobject:@"color_fill2.caf"      fornumber:ksoundcolorfill2]; 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -