1

Hi there I am new to Swift, I am trying to save Longitude and Latitude and place name from map's coordinate object to an Multidimensional array i.e:

Can anyone please help me how do i create these dynamically?

var pinArray[0][Lat] = 51.130231
var pinArray[0][Lon] = -0.189201
var pinArray[0][Place] = "Home"

var pinArray[1][Lat] = 52.130231
var pinArray[1][Lon] = -1.189201
var pinArray[1][Place] = "Office"

var pinArray[2][Lat] = 42.131331
var pinArray[2][Lon] = -1.119201
var pinArray[2][Place] = "Dinner"
3
  • Might need a little more context on this. What does your input data look like? Commented Jan 14, 2016 at 22:59
  • Hi Beau I Looks as shown in question there is longitude, latitude, place name Commented Jan 14, 2016 at 23:00
  • Of course, but are these hard coded values, or are they coming from something in JSON format for example. Could you not just loop through that data? Is that what you want? Commented Jan 14, 2016 at 23:02

3 Answers 3

1

You can make an array of dictionaries, but I suggest using structs instead.

Array of dictionaries

Create an empty array of dictionaries:

var pinArray = [[String:AnyObject]]()

Append dictionaries to the array:

pinArray.append(["lat":51.130231, "lon":-0.189201, "place":"home"])

pinArray.append(["lat":52.130231, "lon":-1.189201, "place":"office"])

But since your dictionaries hold two types of value (Double and String) it will be cumbersome to get the data back:

for pin in pinArray {
    if let place = pin["place"] as? String {
        print(place)
    }
    if let lat = pin["lat"] as? Double {
        print(lat)
    }
}

So, better use structs instead:

Array of structs

Create a struct that will hold our values:

struct Coordinates {
    var lat:Double
    var lon:Double
    var place:String
}

Create an empty array of these objects:

var placesArray = [Coordinates]()

Append instances of the struct to the array:

placesArray.append(Coordinates(lat: 51.130231, lon: -0.189201, place: "home"))

placesArray.append(Coordinates(lat: 52.130231, lon: -1.189201, place: "office"))

It's then easy to get the values:

for pin in placesArray {
    print(pin.place)
    print(pin.lat)
}
Sign up to request clarification or add additional context in comments.

Comments

1

Without more information, this is what I can offer.

var pinArray = [[AnyObject]]()

for location in mapLocations {
    var innerArray = [location["latitude"], location["longitude"], location["place"]]
    pinArray.append(innerArray)
}

1 Comment

That's exactly what i was looking for Beau Thanks
1

Solution using an enum for Lat/Lon/Place (as you don't show us what these are):

enum Pos {
    case Lat
    case Lon
    case Place

    static let allPositions = [Lat, Lon, Place]
}

var myMatrix = [[Pos:Any]]()
myMatrix.append([.Lat: 51.130231, .Lon: -0.189201, .Place: "Home"])
myMatrix.append([.Lat: 52.130231, .Lon: -1.189201, .Place: "Office"])
myMatrix.append([.Lat: 42.131331, .Lon: -1.119201, .Place: "Dinner"])

/* check */
for (i,vector) in myMatrix.enumerate() {
    for pos in Pos.allPositions {
        print("myMatrix[\(i)][\(pos)] = \(vector[pos] ?? "")")
    }
}
/*
myMatrix[0][Lat] = 51.130231
myMatrix[0][Lon] = -0.189201
myMatrix[0][Place] = Home
myMatrix[1][Lat] = 52.130231
myMatrix[1][Lon] = -1.189201
myMatrix[1][Place] = Office
myMatrix[2][Lat] = 42.131331
myMatrix[2][Lon] = -1.119201
myMatrix[2][Place] = Dinner    */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.