从NSMutableArray中删除重复的坐标

时间:2016-04-20 23:58:51

标签: ios objective-c nsmutablearray

我有一个坐标数组,这些坐标是字符串。因此,一个字符串是lat的坐标,长整数用逗号分隔。

例如:47.551170,18.961630

所以我的数组看起来像这样:

 47.551090,18.961610
 47.551010,18.961600
 47.550910,18.961600
 47.550830,18.961610
 47.550750,18.961640
 47.550690,18.961680
 47.549980,18.962870
 47.549170,18.964170
 47.548950,18.964500
 47.548840,18.964620
 47.548710,18.964690
 47.548470,18.964690
 47.548400,18.964690
 47.547990,18.964720

如果它的lat或long与另一个坐标的纬度或长度匹配,如何删除一个坐标?

4 个答案:

答案 0 :(得分:1)

使用NSMutableSet尝试此操作: -

NSMutableArray *Array1 = @[@"47.551090,18.961610",
@"47.551010,18.961600",
@"47.550910,18.961600",
@"47.550830,18.961610",
@"47.550750,18.961640",
@"47.550690,18.961680",
@"47.549980,18.962870",
@"47.549170,18.964170",
@"47.548950,18.964500",
@"47.548840,18.964620",
@"47.548710,18.964690",
@"47.548470,18.964690",
@"47.548400,18.964690",
@"47.547990,18.964720", nil];

NSMutableSet *uniqueMakes = [NSMutableSet setWithArray:Array1];

现在如果您使用NSLog打印uniqueMakes然后它只打印那些在此之后不重复的唯一标记,您可以在MapView上显示这些值。

如果您想了解有关NSSet的更多信息,请转到此链接http://rypress.com/tutorials/objective-c/data-types/nsset

答案 1 :(得分:0)

您需要将这些字符串转换为CLLocationCoordinate2D个对象,以便您可以比较值而不是字符串。如果你提供==覆盖,那么你可以使用swift的contains方法轻松创建一个独特坐标数组,如下所示:

var coordinates: [CLLocationCoordinate2D] = []
for string in stringArray {
    let coordinateStrings = string.componentsSeparatedByString(",")
    let coordinate = CLLocationCoordinate2D(latitude: Double(coordinateStrings[0])!, longitude: Double(coordinateStrings[1])!)
    coordinates.append(coordinate)
}

func ==(lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
    //If you really do mean lat OR long, then change the && to ||
    return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}

var uniqueCoordinates: [CLLocationCoordinate2D] = []
for coordinate in coordinates {
    if !uniqueCoordinates.contains(coordinate) {
        uniqueCoordinates.append(coordinate)
    }
}

然后,如果你想将这些坐标转换回字符串,你可以这样做:

var uniqueStrings: [String] = []
for coordinate in uniqueCoordinates {
    let uniqueString = String(format: "%f,%f", coordinate.latitude,coordinate.longitude)
    uniqueStrings.append(uniqueString)
}

答案 2 :(得分:0)

使用NSOrderedSetNSMutableSetNSSet只是为了存储不同的值,它会自动忽略重复值。

  

NSMutableSet类声明了一个可变的,无序的不同对象集合的编程接口。

     

NSOrderedSet及其子类NSMutableOrderedSet将编程接口声明为有序的对象集合。

Apple Documentation about NSMutableSetNSOrderedSet Class Reference

中了解详情
NSOrderedSet *uniqueSet = [NSOrderedSet setWithObjects:@"47.551090,18.961610",
@"47.551010,18.961600",
@"47.550910,18.961600",
@"47.550830,18.961610",
@"47.550750,18.961640",
@"47.550690,18.961680",
@"47.549980,18.962870",
@"47.549170,18.964170",
@"47.548950,18.964500",
@"47.548840,18.964620",
@"47.548710,18.964690",
@"47.548470,18.964690",
@"47.548400,18.964690",
@"47.547990,18.964720", nil];

NSLog(@"Unique List : %@",uniqueSet);

通过多次添加任意值来检查它,它只会在您登录时显示一次。

快乐编码:)

答案 3 :(得分:0)

NSArray *Array1 = @[@"47.551090,18.961610",
                           @"47.551010,18.961600",
                           @"47.550910,18.961600",
                           @"47.550830,18.961610",
                           @"47.550750,18.961640",
                           @"47.550690,18.961680",
                           @"47.549980,18.962870",
                           @"47.549170,18.964170",
                           @"47.548950,18.964500",
                           @"47.548840,18.964620",
                           @"47.548710,18.964690",
                           @"47.548470,18.964690",
                           @"47.548400,18.964690",
                           @"47.547990,18.964720"];

NSMutableArray *finalArr=[[NSMutableArray alloc] init];
for (NSString *str in Array1) {
    NSArray *temp=[str componentsSeparatedByString:@","];
    if(temp!=nil && [temp count]==2){
        BOOL isSame=NO;
        for(NSString *s in finalArr){
             NSArray *temp2=[s componentsSeparatedByString:@","];
            if([[temp objectAtIndex:0] isEqualToString:[temp2 objectAtIndex:0]]){
                isSame=YES;
                break;
            }

            if([[temp objectAtIndex:1] isEqualToString:[temp2 objectAtIndex:1]]){
                isSame=YES;
                break;
            }
        }

        if(isSame==NO){
            [finalArr addObject:str];
        }
    }
}
NSLog(@"%@",finalArr);