比较数组 ios

Posted

技术标签:

【中文标题】比较数组 ios【英文标题】:Compare arrays ios 【发布时间】:2012-12-13 08:52:00 【问题描述】:

如何比较两个不同值的数组 数组一个有单词,另一个有图像

【问题讨论】:

这两个数组有什么比较的?或者你想映射这两个数组,以便在选择狗时显示 dog.jpg,请解释.. 是这样的,当我按下一个按钮并显示狗这个词时,狗的图像也必须显示出来 使用一个数组。您可以通过将文件名的子字符串带到句点来轻松获取名称。 【参考方案1】:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:array2 forKeys:array1];

【讨论】:

【参考方案2】:

我认为您需要使用NSDictionary。这就是你这样做的方式(使用新的 Objective C 文字语法)

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

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

NSString *fileName = dictionary[@"dog"];

【讨论】:

【参考方案3】:

单击按钮时,您可以简单地获取该值并搜索图像数组以获得例如匹配的图像名称,

NSString *selValue = @"dog";
for (NSString *obj in imagesArray) 
    if ([obj rangeOfString:selValue].location != NSNotFound) 
        NSString *imageName = obj;
        break;
    

【讨论】:

【参考方案4】:

这不是完全符合您要求的代码,可以用作创意,因为您有图像。

假设你的文字和图片在同一个索引中

我刚刚用名为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);

【讨论】:

以上是关于比较数组 ios的主要内容,如果未能解决你的问题,请参考以下文章

IOS 开发中遍历数组的方法及比较

iOS中数组遍历的方法及比较

iOS中数组遍历的方法及比较

ios开发之--比较两个数组里面的值是否相同

ios - 在运行时比较数组的值,不知道它们会是啥

java里,用IO流拷贝文件,用基本流的数组拷贝800兆的文件,数组里设置多少比较合适?