0

キー値 (myKeys) を保持する配列と、NSString オブジェクト (myStrings) を保持する配列の 2 つの配列があります。両方の配列を使用して、高速列挙を使用して単一の NSDictionary (myDictionary) を設定したいのですが、方法がわかりません。

for (NSNumber *key in myKeys) {

   [self.myDictionary setObject @"here is where the value should go from the 'other' array forKey: key];

}

ここで NSArray オブジェクトをどのように考慮に入れますか?

4

3 に答える 3

5

ドキュメントを確認してください。NSDictionaryは列挙なしでこれを行うことができます。

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:myObjects forKeys:myKeys];

既存の mutableDictionary に値を追加しようとしている場合は、それも可能です。

[mutableDictionary addEntriesFromDictionary:dictionary];
于 2014-01-24T17:54:23.610 に答える
0

for ループを本当に高速化したい場合は、NSEnumerationConcurrent オプションを使用できます。この列挙オプションにより、iDevice で利用可能なすべてのリソースを使用していることを確認できます。

[myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSNumber *key, NSUInteger idx, BOOL *stop) {
    [self.myDictionary setObject @"here is where the value should go from the 'other' array" forKey: key];
}];

同時ループの詳細については、次の投稿を参照してください: NSEnumerationConcurrent を使用する場合

于 2014-01-24T17:57:46.613 に答える
0

通常の for ループを使用し、代わりに object at index を使用するか、独自のカウンターを作成して同じことを行うことをお勧めします。ただし、foreach を保持したいが、独自のカウンターを作成したくない場合は、次のようにすることができます。

[self.myDict setObject:[myStrings objectAtIndex:[myKeys indexOfObject:key]] 
                forKey: key];
于 2014-01-24T17:54:03.147 に答える