0

キーを自動インクリメントし、NSMutableDictionary にしたい。

私はそれをやろうとしましたが、うまくいきませんでした:

NSMutableDictionary *array = [[NSMutableDictionary alloc] init];

int testAutoIndex = 0;
[array setObject:[NSNumber numberWithInt:testAutoIndex++] forKey:@"index"];
[array release];

ありがとう :)

4

2 に答える 2

0

たとえばNエントリの辞書を作成する場合は、コードを使用して作成できます。

int N = 100; // or what ever number you want
NSArray *arrayOfObjectsYouWantToPumpIntoDictionary = ....;
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:100];

for(int i=0;i<N;i++){

    NSString *key = [NSString stringWithFormat:@"%d"i];
    [mutableDictionary setObject:[arrayOfObjectsYouWantToPumpIntoDictionary objectAtIndex:i] forKey:];
}

// Later some where else if you want to retrieve object with key of x (x can be 1, or 2 or what ever value), you can do

 objectToBeRetrieved = [mutableDictionary objectForKey:[NSString stringWithFormat:@"%d",x];
于 2012-11-02T21:20:46.007 に答える
0

これを使って:

NSMutableDictionary *array = [[NSMutableDictionary alloc] init];

int testAutoIndex = 0;
[array setObject:[NSNumber numberWithInt:++testAutoIndex] forKey:@"index"];
[array release];
于 2012-11-02T00:58:26.940 に答える