Ios 将唯一索引值与目标C中的另一个数组进行比较
有一个数组在单个数组中具有相同的对象,我需要将这些数组的索引与另一个数组进行比较。。帮我个忙。 比如:Ios 将唯一索引值与目标C中的另一个数组进行比较,ios,objective-c,iphone,arrays,nsmutablearray,Ios,Objective C,Iphone,Arrays,Nsmutablearray,有一个数组在单个数组中具有相同的对象,我需要将这些数组的索引与另一个数组进行比较。。帮我个忙。 比如: NSMutableArray *latArray = [NSMutableArray arrayWithObjects:@“43.20,@“43.23”,@“43.24”,@“43.20”,nil]; NSMutableArray *lngArray = [NSMutableArray arrayWithObjects:@“76.90”,@“76.94
NSMutableArray *latArray =
[NSMutableArray arrayWithObjects:@“43.20,@“43.23”,@“43.24”,@“43.20”,nil];
NSMutableArray *lngArray =
[NSMutableArray arrayWithObjects:@“76.90”,@“76.94”,@“76.92”,@“76.90”,nil];
NSMutableArray *imagesArray =
[[NSMutableArray alloc] initWithObjects:@"1.jpg", @"2.jpg”,@“3.jpg”,@“4.jpg”,nil];
resultResult = @"1.jpg", @“4.jpg” // because the index 0 and index 3 same values in both array.
我会将你的坐标包装成位置对象,并将它们用作字典中的键。这将允许检查重复的坐标,如下所示:
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [imagesArray count]; i++)
{
// Wrap coordinates into a NSValue object
// (CLLocationCoordinate2D is a C-struct that cannot be used as a dictionary key)
// (CLLocation also does not implement required methods to be usable as a dictionary key)
NSValue *loc = [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(
((NSNumber)[latArray objectAtIndex:i]).doubleValue,
((double)[lngArray objectAtIndex:i]).doubleValue)];
// 1. If you only want the first occurrence of a specific location, use this:
if ([results objectForKey:loc] == nil)
{
[results setObject:[imagesArray objectAtIndex:i] forKey:loc];
}
// 2. Or, if you want the last occurrence of a specific location, use this:
[results setObject:[imagesArray objectAtIndex:i] forKey:loc];
}
NSMutableDictionary*results=[[NSMutableDictionary alloc]init];
对于(int i=0;i<[imagesArray计数];i++)
{
//将坐标包装到NSValue对象中
//(CLLocationCoordinate2D是不能用作字典键的C结构)
//(CLLocation也没有实现可用作字典键的所需方法)
NSValue*loc=[NSValue VALUE WITHMK坐标:CLLocationCoordinate2DMake(
((NSNumber)[latArray objectAtIndex:i]).doubleValue,
((双重)[lngaray objectAtIndex:i]).doubleValue)];
//1.如果您只希望第一次出现特定位置,请使用以下命令:
if([results objectForKey:loc]==nil)
{
[结果setObject:[imagesArray objectAtIndex:i]forKey:loc];
}
//2.或者,如果您想要某个特定位置的最后一次出现,请使用以下命令:
[结果setObject:[imagesArray objectAtIndex:i]forKey:loc];
}
我认为您正在尝试检查数组中的相同对象。如果是,请执行以下操作
for(int i=0;i<yourarray.count;i++)
{
NSString *yourstring=[yourarray objectatindex:i];
for(int k=0;k<yourarray.count;k++)
{
if(i!=k)
{
NSString *yourstring2=[yourarray objectatindex:k];
if([yourstring isEqualtostring yourstring2])
{
//now you got equal objects. do what ever you want here
}
}
}
}
for(int i=0;iquestion不清楚,您想要什么?@saif:检查数组中的同一对象和另一个数组中的同一索引,显示比较imagesarray的同一索引值的结果。“5.jpg”在哪里来自?您可能对此链接感兴趣:因此,您可能会使用NSValue
/CLLocationCoordinate2D
创建一个新数组,以实际用于比较。