带字符串常量和ID的NSDictionary

时间:2012-06-05 17:26:07

标签: objective-c ios nsdictionary constants

我正在为这个问题寻找好的解决方案。 我需要大约20多个字符串常量(资源文件名)。但我需要:

  1. 组常量(文件名)
  2. 迭代选定的常量组
  3. 按ID
  4. 访问常量

    所以我找到的最佳解决方案是为每个具有常量值和id的组创建常量NSDictionary。 我想使用enum来存储常量ID,但是int不能在NSDictionary中用作键。

    typedef enum {
        kSoundColorSelection1 = 0,
        kSoundColorSelection2,
        kSoundColorSelection3,
        kSoundFill1,
        kSoundFill2,
        kSoundSizeSelectionBig,
        kSoundSizeSelectionMedium
    } ACSoundId;
    

    “最简单”方式明确地将int转换为NSNumber

    templatesSounds = [[NSDictionary alloc] initWithObjectsAndKeys:@"album_close.caf",[NSNumber numberWithInt:kSoundAlbumClose],
                     @"album_open.caf", [NSNumber numberWithInt:kSoundAlbumOpen], nil];
    

    在这种情况下还有其他方法可以保持常量吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

有两种方法可以使这更容易。

解决方案1 ​​ 如果您使用ACSoundId作为NSDictionary的键,您可以使用NSArray:

typedef enum {
    kSoundColorSelection1      = 0,
    kSoundColorSelection2      = 1,
    kSoundColorSelection3      = 2,
    kSoundFill1                = 3,
    kSoundFill2                = 4,
    kSoundSizeSelectionBig     = 5,
    kSoundSizeSelectionMedium  = 6
} ACSoundId;

按照上述枚举中定义的顺序将声音存储在数组中:

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];

由于声音的索引与枚举的值相关,因此它的工作方式类似于NSDirectory:

queuedSound = [templatesSounds objectAtIndex:kSoundColorSelection2];

解决方案2 或者,您可以创建一个类别,以便更容易将整数用作NSDictionary中的键:

定义类别:

@interface NSMutableDictionary (NSNumberDictionary)
- (void) setObject:(id)anObject forNumber:(NSInteger)aNumber;
- (void) removeObjectForNumber:(NSInteger)aNumber;
- (id)   objectForNumber:(NSInteger)aNumber;
@end

实施类别:

@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

使用类别:

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];

答案 1 :(得分:1)

如果你的“键”总是整数,为什么不使用普通的C数组?

NSString **templatesSounds ;

int numConstants = 10;
templatesSounds = calloc(numConstants , sizeof(NSString*));

templatesSounds[kSoundAlbumOpen] = @"album_open.caf";

NSString* soundName = templatesSounds [kSoundSizeSelectionMedium];

//...
free (templatesSounds);