【问题标题】:Compare the unique index value with another array in objective C将唯一索引值与目标 C 中的另一个数组进行比较
【发布时间】:2015-02-27 09:57:42
【问题描述】:

在单个数组中有一个具有相同对象的数组,我需要将这些数组的索引与另一个数组进行比较..帮帮我。 比如:

 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.

【问题讨论】:

  • 问题不清楚,你想要什么..?
  • @saif:检查数组中的相同obj和另一个数组的相同索引,显示相同索引值比较imagesarray的结果。
  • “5.jpg”从何而来?
  • 您可能对此链接感兴趣:stackoverflow.com/questions/3757540/… 并可能因此创建一个带有NSValue/CLLocationCoordinate2D 的新数组以实际用于比较。

标签: ios objective-c iphone arrays nsmutablearray


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 2021-04-17
      • 1970-01-01
      • 2013-10-23
      • 2016-07-06
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多