1

問題の良い解決策を探しています。約 20 個以上の文字列定数 (リソース ファイル名) が必要です。しかし、私はする必要があります:

  1. グループ定数 (ファイル名)
  2. 選択した定数グループを繰り返します
  3. ID による定数へのアクセス

したがって、私が見つけた最善の解決策は、定数値と ID を持つ各グループに対して定数 NSDictionary を作成することでした。定数 ID の格納に使用したかったenumのですが、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];

この場合、定数を維持する他の方法はありますか? ありがとう!

4

2 に答える 2

2

これを簡単にする方法は2つあります。

解決策 1ACSoundIdを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];
于 2012-06-05T17:43:45.723 に答える
1

「キー」が常に整数である場合は、通常のC配列を使用してみませんか?

NSString **templatesSounds ;

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

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

NSString* soundName = templatesSounds [kSoundSizeSelectionMedium];

//...
free (templatesSounds);
于 2012-06-05T17:45:11.160 に答える