I have a loop that is taking taking elements of an array which is postcodes and changing them in a for loop into latitude, longitude, title and postcode and than creating and appending an array of dictionaries with the result. there is 3 postcodes in the original array and it shows this with a print but for some reason the loop isn't only looping 3 times it is doing it quite a few times this is giving me duplicate data, i have looked at removing the duplicates and this works in some way by leaving me with 3 results but sometimes the results are ok where the postcode and the title are all different but in some cases it comes back with the same title in with 2 different postcodes.
can anyone help with this point me in the right direction?
self.places = self.pCodes
// Loop start
for eachAddress in self.places {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(eachAddress) {
placemarks, error in
let placemark = placemarks?.first
let lat = placemark?.location?.coordinate.latitude
let lon = placemark?.location?.coordinate.longitude
let postPcode = eachAddress
let locationTitle = self.cNames[self.lt]
let latLon = ["Post Code": postPcode, "title": locationTitle, "latitude":lat!, "longitude": lon!] as [String : Any]
self.posts.append(latLon)
// adding 1 to lt to pick the next title in the array
if self.lt < (self.pCodes.count - 1) {
self.lt += 1
}else {
self.lt = (self.pCodes.count - 1)
}
// removing duplicates
var set = Set<String>()
let arraySet: [[String : Any]] = self.posts.flatMap {
guard let name = $0["Post Code"] as? String else {return nil }
return set.insert(name).inserted ? $0 : nil
}
self.postsFinal = arraySet
print("Tony Places \(self.places)")
print("Tony: postsFinal \(self.postsFinal)")
self.showSightingsOnMap()
}
}
and this is the print out I get.
Tony Places ["OL8 2TT", "PO1 5DS", "PO1 5JA"] Tony: postsFinal [["latitude": 53.514649900000002, "Post Code": "OL8 2TT", "title": "JDFG VBF", "longitude": -2.1052824000000001]] Tony Places ["OL8 2TT", "PO1 5DS", "PO1 5JA"] Tony: postsFinal [["latitude": 53.514649900000002, "Post Code": "OL8 2TT", "title": "JDFG VBF", "longitude": -2.1052824000000001]] Tony Places ["OL8 2TT", "PO1 5DS", "PO1 5JA"] Tony: postsFinal [["latitude": 53.514649900000002, "Post Code": "OL8 2TT", "title": "JDFG VBF", "longitude": -2.1052824000000001]] Tony Places ["OL8 2TT", "PO1 5DS", "PO1 5JA"] Tony: postsFinal [["latitude": 53.514649900000002, "Post Code": "OL8 2TT", "title": "JDFG VBF", "longitude": -2.1052824000000001], ["latitude": 50.800523599999998, "Post Code": "PO1 5DS", "title": "QWER QWE", "longitude": -1.0723248000000001]] Tony Places ["OL8 2TT", "PO1 5DS", "PO1 5JA"] Tony: postsFinal [["latitude": 53.514649900000002, "Post Code": "OL8 2TT", "title": "JDFG VBF", "longitude": -2.1052824000000001], ["latitude": 50.800523599999998, "Post Code": "PO1 5DS", "title": "QWER QWE", "longitude": -1.0723248000000001], ["latitude": 50.802415099999997, "Post Code": "PO1 5JA", "title": "QWER QWE", "longitude": -1.0726989]] Tony Places ["OL8 2TT", "PO1 5DS", "PO1 5JA"] Tony: postsFinal [["latitude": 53.514649900000002, "Post Code": "OL8 2TT", "title": "JDFG VBF", "longitude": -2.1052824000000001], ["latitude": 50.800523599999998, "Post Code": "PO1 5DS", "title": "QWER QWE", "longitude": -1.0723248000000001], ["latitude": 50.802415099999997, "Post Code": "PO1 5JA", "title": "QWER QWE", "longitude": -1.0726989]]