将唯一索引值与目标C中的另一个数组进行比较

时间:2015-02-27 09:57:42

标签: 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”,@“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.

2 个答案:

答案 0 :(得分:0)

我会将您的坐标包装到位置对象中,并将它们用作字典中的键。这将允许检查重复的坐标,如下所示:

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

答案 1 :(得分:0)

我认为您正在尝试检查数组中的相同对象。如果是这样,请执行以下操作。

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