2012-12-13 195 views
-1

如何比較2個陣列,不同的價值 陣列一個有句話連接其他具有圖像比較陣列IOS

+0

什麼與THES比較兩個數組?或者你想映射這兩個數組,這樣,當狗被選中dog.jpg顯示,請解釋.. –

+0

這是類似的東西,當我按下按鈕,並顯示狗的形象比狗的圖像也必須顯示 – T1992

+1

使用一個數組。您可以通過截取文件名的子字符串來輕鬆獲取名稱。 – borrrden

回答

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 [email protected]"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 

如果文字和圖像不在anyorder

//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 [email protected]"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); 
0

我認爲你需要使用一個NSDictionary。這是如何做到這一點(使用新的目標C文字語法)

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

要檢索從這個字典「狗」的圖像文件名這樣做:

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

當按鈕被點擊你可以簡單地採取價值和搜索到的圖像陣列以獲得例如匹配圖像名稱,

NSString *selValue = @"dog"; 
for (NSString *obj in imagesArray) { 
    if ([obj rangeOfString:selValue].location != NSNotFound) { 
     NSString *imageName = obj; 
     break; 
    } 
} 
1
NSDictionary *dict = [NSDictionary dictionaryWithObjects:array2 forKeys:array1];