从 NSMutableArray 中删除重复的坐标
Remove duplicate coordinates from NSMutableArray
我有一个坐标数组,它是字符串。所以一个字符串是一个坐标,纬度和经度用逗号分隔。
例如: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
如果一个坐标的纬度或经度与数组中另一个坐标的纬度或经度匹配,我该如何删除该坐标?
您需要将这些字符串转换为 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)
}
使用NSOrderedSet
、NSMutableSet
或NSSet
非常简单,只存储不同的值,它会自动忽略重复值。
The NSMutableSet class declares the programmatic interface to a mutable, unordered collection of distinct objects.
NSOrderedSet and its subclass, NSMutableOrderedSet, declare the programmatic interfaces to an ordered collection of objects.
在Apple Documentation about NSMutableSet
and NSOrderedSet 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);
通过多次添加任何值来检查它,它只会在您登录时显示一次。
编码愉快:)
尝试使用 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 的信息,请转到此 link http://rypress.com/tutorials/objective-c/data-types/nsset .
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);
我有一个坐标数组,它是字符串。所以一个字符串是一个坐标,纬度和经度用逗号分隔。
例如: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
如果一个坐标的纬度或经度与数组中另一个坐标的纬度或经度匹配,我该如何删除该坐标?
您需要将这些字符串转换为 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)
}
使用NSOrderedSet
、NSMutableSet
或NSSet
非常简单,只存储不同的值,它会自动忽略重复值。
The NSMutableSet class declares the programmatic interface to a mutable, unordered collection of distinct objects.
NSOrderedSet and its subclass, NSMutableOrderedSet, declare the programmatic interfaces to an ordered collection of objects.
在Apple Documentation about NSMutableSet
and NSOrderedSet 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);
通过多次添加任何值来检查它,它只会在您登录时显示一次。
编码愉快:)
尝试使用 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 的信息,请转到此 link http://rypress.com/tutorials/objective-c/data-types/nsset .
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);