Tony Merritt
Tony Merritt

Reputation: 1197

Append an array of dictionaries

I am trying to append an array of dictionaries with latitude and longitude.

This is because I want to add annotations to a map, But I am getting the data from Firebase and need to store them in an array.

I get them from an array of postcode and put them into a dictionary, this all prints fine but now I am trying to add them to a new array.

I want to create an array of dictionaries only. With the final result in posts. i.e [["latitude: 873648264, "longitude": 9849823658293753], [latitude: 873648264, "longitude": 9849823658293753 ]]

Below is my code so far, I have looked around for answers and there are so many but I cannot make head nor tale of it.

var posts = [[Dictionary<String,Double>]]()

var latitude = Dictionary<String,Double>()
var longitude = Dictionary<String,Double>()
latitude = ["latitude":lat!]
longitude = ["longitude":lon!]
var latLon = ["latitude":latitude, "longitude":longitude]

posts.append(latLon)

//Updated question from here:

What aim trying to achieve is to replace this code with the lat and lon of my own data from firebase.

let locations = [
["title": "New York, NY",    "latitude": 40.713054, "longitude": -74.007228],
["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344],
["title": "Chicago, IL",     "latitude": 41.883229, "longitude": -87.632398]
]

for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location["title"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
mapView.addAnnotation(annotation)
}

Upvotes: 1

Views: 2069

Answers (1)

Pruthvikar
Pruthvikar

Reputation: 439

For an array of dictionaries only, this should suffice

var posts = [[String: Double]]()

var latLon = ["latitude":lat!, "longitude": lon!]

posts.append(latLon)

Upvotes: 2

Related Questions