-1

値の異なる 2 つの配列を比較する方法

4

4 に答える 4

1
NSDictionary *dict = [NSDictionary dictionaryWithObjects:array2 forKeys:array1];
于 2012-12-24T16:51:31.970 に答える
0

を使用する必要があると思いますNSDictionary。これがその方法です(新しいObjective Cリテラル構文を使用)

NSDictionary *dictionary = @{
        @"dog" : @"dog.jpg", 
        @"apple" : @"apple.jpeg",
        @"clown" : @"clown.gif"
};

この辞書から「犬」の画像ファイル名を取得するには、次のようにします。

NSString *fileName = dictionary[@"dog"];
于 2012-12-13T09:22:55.303 に答える
0

ボタンをクリックすると、その値を取得して画像配列を検索し、一致する画像名を取得できます。

NSString *selValue = @"dog";
for (NSString *obj in imagesArray) {
    if ([obj rangeOfString:selValue].location != NSNotFound) {
        NSString *imageName = obj;
        break;
    }
}
于 2012-12-13T09:23:49.533 に答える
0

これは、要件に合わせて完全に機能するコードではありません。イメージがあるので、アイデアとして使用できます。

あなたの単語と画像が同じインデックスにあると仮定します

A.jpg という名前の文字列で同様の状況を実装しました. 考え方は同じです. それに応じて変換する必要があります.

NSMutableArray *words=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D", nil];
NSMutableArray *images=[[NSMutableArray alloc]initWithObjects:@"A.jpg",@"B.jpg",@"C.jpg",@"D.jpg", nil];

id selectedWord=@"C";//This is storing which word you have selected
id selectedImage=[images objectAtIndex:[words indexOfObject:selectedWord]];//this will store the image
NSLog(@"%@",selectedImage);//now you can display the image in imageview

言葉もイメージも順不同なら

//words array is of no use, you can simply find which word you selected by extracting before "." , but as I am not aware of exact requirement I have left words array.
NSMutableArray *words=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D", nil];
NSMutableArray *images=[[NSMutableArray alloc]initWithObjects:@"B.jpg",@"D.jpg",@"C.jpg",@"A.jpg", nil];

id selectedWord=@"B";

NSInteger indexOfSelectedWord;
for (NSString *imageName in images) {

    if ([[[imageName componentsSeparatedByString:@"."]objectAtIndex:0]isEqualToString:selectedWord]) {
        indexOfSelectedWord=[images indexOfObject:imageName];
    }
}

id selectedImage=[images objectAtIndex:indexOfSelectedWord];

NSLog(@"%@ & %@",selectedWord ,selectedImage);
于 2012-12-13T09:15:17.977 に答える