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
}
}
theKeyandtheValueto contain??theKeybe "Lat"? That makes no sense, you have an array, there is no "Lat" to be found anywhere.