2017-09-20 10 views
-4

私は子供のためのアプリを開発しており、{"A":[apple、Ant、Airplane]、 "B":[ ...]、...}。そしてそれから、私はApple、A、飛行機のためのAとして値を取得する必要があります。 私はコードを試してみました:objective-cで配列の辞書を作成する

alphabetsStoreArray = [[NSMutableArray alloc]initWithObjects:@"a.png",@"b.png",@"c.png",@"d.png",@"e.png",@"f.png",@"g.png",@"h.png",@"i.png",@"j.png",@"k.png",@"l.png",@"m.png",@"n.png",@"o.png",@"p.png",@"q.png",@"r.png",@"s.png",@"t.png",@"u.png",@"v.png",@"w.png",@"x.png",@"y.png",@"z.png", nil]; 

しかし、私は後述するように取得する必要があります。

答えて

1

あなたが好き行うことができ、

NSArray *arrA = [[NSArray alloc]initWithObjects:@"Apple",@"AeroPlane",@"Ant", nil]; 

NSArray *arrB = [[NSArray alloc]initWithObjects:@"Ball",@"Baloon",@"Banana", nil]; 

NSArray *arrC = [[NSArray alloc]initWithObjects:@"Cat",@"Car",@"Cow", nil]; 

NSDictionary *resultDictionary = @{@"A" : arrA, @"B" : arrB, @"C" : arrC} ; 
2

のようなものを試してみてください。この

NSDictionary *data = @{@"A":@[@"Apple",@"Ant",.....], 
         @"B":@[@"Ball",@"Bat",.....], 
         @"C":@[@"Call",@"Cat",.....], 
         . 
         . 
         . 
         }; 
+0

私はまた、どのようにVALUES – jesti

+0

から値りんごを取得する必要がありthanks.but。あなた自身のためにそれを試してみてください。あなたはそれを辞書から読むだけで済む。このように[data valueForKey:@ "a"] – jesti

+0

を取得するために、それはここから本当に簡単だということ – rv7284

0

まず第一に、私は(これらの値が固定されている場合またはグローバル定数)より、それを作るために、いくつかのローカル変数を使用することをお勧めしますアルファベットの各値に対してkeyNSStringとするNSMutableDictionaryを作成することができます。 valueを配列にすることができます。

NSMutableDictionary<NSString *, NSArray<NSString *>> *alphabetDict = [[NSMutableDictionary alloc] init]; 

// Then you can add each array to the dictionary. 
// For example: 

NSString *keyA = @"A"; 
NSArray<NSString *> *arrayForA = @[ @"Aardvark", @"Apple" ]; 
[alphabetDict setValue:arrayForA forKey:keyA]; 

// Repeat to add each array to the dict. 
-1

目的のcで簡単にマッチを組み合わせることができます。

NSMutableDictionary *alphabets = [[NSMutableDictionary alloc] init]; 
alphabets["a"] = @["apple", "ant", "airplane", nil]; 
aplhabets["b"] = @["boy", "big", nil]; 
+0

なぜdownvote ?? – cocoaNoob

+0

「申し訳ありませんが、コードに構文上の問題があります!!」 - あなたはこの行を答えに書いています!あなたがコードに問題があることを知っているなら、あなたは答えを書くべきではありません!また、編集する前にコードに問題があります。それが理由で投票しています! Btwあなたは答えを編集しましたので、私はそれを取り下げています! – Lion

3

使用このコード

NSMutableArray *arrForA = [[NSMutableArray alloc] initWithObjects:@"Apple", @"Ant", @"Arc", nil]; 
NSMutableArray *arrForB = [[NSMutableArray alloc] initWithObjects:@"Ball", @"Bat", @"Box", nil]; 
NSMutableDictionary *dictAlpha = [[NSMutableDictionary alloc] initWithObjectsAndKeys: arrForA, @"A", arrForB, @"B", nil]; 
NSLog(@"%@",dictAlpha); // 1 

NSArray *keys=[dictAlpha allKeys]; 


for (int i = 0; i< keys.count; i++) { 

    NSArray *arrVal = [dictAlpha objectForKey:keys[i]]; 

    for (int j=0; j<arrVal.count; j++) { 
     NSLog(@"%@ for %@", keys[i], arrVal[j]); // 2 
    } 

} 

出力

1.

enter image description here

2.

enter image description here

+0

あなたが条件を提供することができます – jesti

+0

@ jesti If条件? –

+0

if文の条件で上記のコードが必要です – jesti

1
// A dictionary object 
NSDictionary *dict; 

// Create array to hold dictionaries 

NSMutableArray *arrayOfDictionaries = [NSMutableArray array]; 

// Create three dictionaries 

dict = [NSDictionary dictionaryWithObjectsAndKeys: 
        // Key value pairs 
        @"Bock", style,  
        @"Deep Gold", appearance, 
        [NSNumber numberWithInt:25], hopProfile, 
        nil]; 
arrayOfDictionaries addObject:dict]; 

dict = [NSDictionary dictionaryWithObjectsAndKeys: 
        // Key value pairs 
        @"India Pale Ale (IPA)", style, 
        @"Copper", appearance, 
        [NSNumber numberWithInt:50], hopProfile, 
        nil]; 

[arrayOfDictionaries addObject:dict]; 

dict = [NSDictionary dictionaryWithObjectsAndKeys: 
        @"Stout", style, 
        @"Jet Black", appearance, 
        [NSNumber numberWithInt:35], hopProfile, 
        nil]; 

[arrayOfDictionaries addObject:dict]; 

NSLog(@"array of dictionaries: %@", arrayOfDictionaries); 
関連する問題