Tuples are not easy to work with, I recommend wrapping your data into a custom object first:
import CoreLocation
struct MyLocation: Hashable {
let name: String
let coordinate: CLLocation
init(tuple: (String, Double, Double)) {
name = tuple.0
coordinate = CLLocation(latitude: tuple.1, longitude: tuple.2)
}
public static func == (lhs: MyLocation, rhs: MyLocation) -> Bool {
return
lhs.name == rhs.name
&& lhs.coordinate.distance(from: rhs.coordinate) < 1
}
public var hashValue: Int {
return name.hashValue
}
}
As you can see, I have already declared Equatable and also Hashable for easy indexing.
Then we can use a simple Array extension:
extension Array where Element: Hashable {
func distinct() -> [Element] {
var uniqueValues: Set<Element> = []
return self.filter {
let (inserted, _) = uniqueValues.insert($0)
return inserted
}
}
}
And use it on our data:
var locations = [("Location_A", 49.5858, 9.123456), ("Location_B", 49.5858, 9.123456), ("Location_A", 49.5855, 9.123450)]
let myLocations = locations
.map { MyLocation(tuple: $0) }
.distinct()
print(myLocations)
Note that I have defined equality for two objects when they are closer than 1 meter. That will be slower than simply comparing longitude against longitude and latitude against latitude, but it will be also more precise.