-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"
};

要从此字典中检索“dog”的图像文件名,请执行以下操作:

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 回答