2

I'm trying to loop through array and get the coordinates but it's giving me an error and I can't access my array. The data is passed through a seque and if I print it shows it's working so the data is transferred correctly but can't work out why I can't iterate through the array and pull out the latitude and longitude.

This is the output in the console.

DeliveryDestinations(NameOrBusiness: Optional("In-N-Out Burger"), FirstLineAddress: Optional("550 Newhall Dr"), SecondLineAddress: Optional(" United States"), CityLineAddress: Optional(" San Jose"), PostcodeLineAddress: Optional(" CA 95110"), DistanceToDestination: Optional(9.2807823200000001), Lat: Optional(37.350253606833043), Long: Optional(-121.92182779312132))

import UIKit
import MapKit
import CoreLocation

class DeliveryLocationsVC: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    var addressArr = [DeliveryDestinations]()


    override func viewDidLoad() {
        super.viewDidLoad()

        print(addressArr)


navigationItem.title = "Delivery Location"
        // Do any additional setup after loading the view.

        for (theKey, theValue) in addressArr { //<-- error can't access the elements in array
            if (theKey == "Lat") {
                if let coordinates:DeliveryDestinations = theValue {
                print (coordinates)
            }
            print("Item \(theKey): \(theValue)")
            }
        }
    }
}

My array is coming from a struct below

import Foundation
import MapKit

struct DeliveryDestinations {
    var NameOrBusiness: String?
    var FirstLineAddress: String?
    var SecondLineAddress: String?
    var CityLineAddress: String?
    var PostcodeLineAddress: String?
    var DistanceToDestination: CLLocationDistance?
    var Lat: Double?
    var Long: Double?


    init(NameOrBusiness: String?, FirstLineAddress: String?, SecondLineAddress: String?, CityLineAddress: String?, PostCodeLineAddress: String?, DistanceToDestination: CLLocationDistance?, Lat: Double?, Long: Double? ) {
        self.NameOrBusiness = NameOrBusiness
        self.FirstLineAddress = FirstLineAddress
        self.SecondLineAddress = SecondLineAddress
        self.CityLineAddress = CityLineAddress
        self.PostcodeLineAddress = PostCodeLineAddress
        self.DistanceToDestination = DistanceToDestination
        self.Lat = Lat
        self.Long = Long

    }

}
4
  • What do you expect theKey and theValue to contain?? Commented Dec 27, 2017 at 15:21
  • @luk2302 key will be Lat and value will be the coordinate same for long will be the key and value will be coordinate. Try to put this into array so i can populate the map with pins based on this coordinates. Commented Dec 27, 2017 at 15:23
  • Why should theKey be "Lat"? That makes no sense, you have an array, there is no "Lat" to be found anywhere. Commented Dec 27, 2017 at 15:24
  • @luk2302 is not this ones Lat: Optional(37.350253606833043), Long: Optional(-121.92182779312132)) key and value pairs ? Commented Dec 27, 2017 at 15:25

1 Answer 1

3

You have an Array (and not a Dictionary) so you need to do is iterate like this:

for object in addressArr {
    ...
    // get coordinates from object
    let lat = object.Lat
    let long = object.Long
}

Or if you need index of a table row as well you can do:

for (i, object) in addressArr.enumerated() {
    ...
    // get coordinates from object
    let lat = object.Lat
    let long = object.Long
    print ("Destination at index \(i) has coordinate: (\(lat), \(long))")
}

How did I know it was a Array?

Notation in the code

 var addressArr = [DeliveryDestinations]()

allways mean, that this is an array of objects DeliveryDestinations. When you print it out, you get print of the object in the array. In your case, this object has a "dictionary like" description, so the print function print it like this.

In every object, with description function you can specify how the text would look like when you print on object (or array of those objects)

Sign up to request clarification or add additional context in comments.

3 Comments

Zedravec This worked perfectly for me both solutions are perfect. One question is where i messed up how did you know that this was an array and not dictionary because here i got confused. Lat: Optional(37.350253606833043), Long: Optional(-121.92182779312132)) is this not a key-value pair?
@QuickSilver var addressArr = [DeliveryDestinations](), that is an array, period. No matter what it contains and how those things decide to convert itself to a string, it will remain an array.
@luk2302 Thnks for explaining that sorry I'm fairly new to this so I'm still learning. Much appreciate your help guys.

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.