2009-06-18 7 views
17

NSMutableDictionaryのキーとしてenumを使用できませんか?私は経由で辞書に追加しようとするとNSDictionaryのキーとしてtypedef列挙型?

は:

error: incompatible type for argument 2 of 'setObject:forKey:'

は一般的に、私は私のキー名としてNSStringのを使用するためにキャストを必要としない:

[self.allControllers setObject:aController forKey:myKeyType]; 

私はエラーを取得します'id'しかし、エラーを消すために、私はそれをしていた。キャスティングはここで正しく動作しているのか、キーとしての列挙型は悪い考えですか?

私の列挙型は、次のように定義されます

typedef enum tagMyKeyType 
{ 
    firstItemType = 1, 
    secondItemType = 2 
} MyKeyType; 

と辞書が定義され、適切なとして割り当て:

NSMutableDictionary *allControllers; 

allControllers = [[NSMutableDictionary alloc] init]; 
+0

なぜアレイを使用しないのですか? – user102008

答えて

21

NSNumberに列挙型を格納することはできますが、 (ちょうど列挙型ではありません)

[allControllers setObject:aController forKey:[NSNumber numberWithInt: firstItemType]]; 

Cocoaでは、const NSStringsがよく使われます。 .Hでは、あなたのようなものを宣言します:

NSString * const kMyTagFirstItemType; 
NSString * const kMyTagSecondtItemType; 

をと.mファイルにあなたは、あなたが辞書でキーとして使用することができます

NSString * const kMyTagFirstItemType = @"kMyTagFirstItemType"; 
NSString * const kMyTagSecondtItemType = @"kMyTagSecondtItemType"; 

を置きます。

[allControllers setObject:aController forKey:kMyTagFirstItemType]; 
+2

ここでは、.hファイルの 'extern'キーワードを使用したいと思います。 –

+0

また、 'extern 'にtypedefされていると思われるキーワードとして' FOUNDATION_EXPORT'を使用することもできます。 –

3

いいえ、あなたはできません。メソッドのシグネチャを見てください:idオブジェクトを指定します。列挙型はスカラーです。あなたはあるものから他のものにキャストすることはできませんし、それが正しく機能することを期待してください。オブジェクトを使用する必要があります。

+5

これは本当です。しかし、NSNumberに列挙型の値をラップし、それをキーとして使用することができます。 – LBushkin

4

これは古い質問ですが、より現代的な構文を使用するように更新することができます。私たちは、内部NSIntegerのように表す列挙型を持っているので、私たちは辞書のキーとして格納するNSNumber秒にそれらをボックスすることができ、その後

typedef NS_ENUM(NSInteger, MyEnum) { 
    MyEnumValue1 = -1, // default is 0 
    MyEnumValue2, // = 0 Because the last value is -1, this auto-increments to 0 
    MyEnumValue3 // which means, this is 1 
}; 

:まず、iOSの6のように、Appleは列挙型は以下のように定義することをお勧めします。

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; 

// set the value 
dictionary[@(MyEnumValue1)] = @"test"; 

// retrieve the value 
NSString *value = dictionary[@(MyEnumValue1)]; 

EDIT:どのようにこのメソッドを介して、この目的のために

をシングルトン辞書を作成するには、これを支援するためにシングルトン辞書を作成することができます。あなたが持っているものは何でもファイルの先頭には、使用することができます:

static dispatch_once_t onceToken; 
// this, in combination with the token above, 
// will ensure that the block is only invoked once 
dispatch_async(&onceToken, ^{ 
    enumTranslations = @{ 
     @(MyEnumValue1): @"test1", 
     @(MyEnumValue2): @"test2" 
     // and so on 
    }; 
}); 




// alternatively, you can do this as well 
static dispatch_once_t onceToken; 
// this, in combination with the token above, 
// will ensure that the block is only invoked once 
dispatch_async(&onceToken, ^{ 
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; 
    mutableDict[@(MyEnumValue1)] = @"test1"; 
    mutableDict[@(MyEnumValue2)] = @"test2"; 
    // and so on 

    enumTranslations = mutableDict; 
}); 

static NSDictionary *enumTranslations; 

を次に、あなたのinitまたはviewDidLoadに(あなたはUIコントローラでこれをやっている場合)、あなたはこれを行うことができます

この辞書を外部から見えるようにするには、静的宣言をヘッダー(.h)ファイルに移動します。

関連する問題