title: Swift Appending Coordinates into Array and Adding to Map
body: <p>My intentions of this app is to allow the user to input the any set of coordinates in one tab of a tab controller, and in another tab, the coordinates will be placed in a pin with an annotation. </p>
  
  <p>The method I have been trying to implement is to use a global array that appends the input coordinates and called in the other class file (with the mapView). I have had a slew of problems in trying to iterate through the array to place the pins, and I am not sure what is wrong.</p>
  
  <p>Input Info class file:</p>
  
  <pre><code>import UIKit
  import CoreLocation
  
  var locations: [Dictionary&lt;String, Any&gt;] = [] // here I initialize my array
  
  class OtherVC: UIViewController {
  
      @IBOutlet weak var latitudeField: UITextField!
      @IBOutlet weak var longitudeField: UITextField!
  
      @IBOutlet weak var titleTextField: UITextField!
      var coordinates = [CLLocationCoordinate2D]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
  
  
   // This IBOutlet takes the coordinate input information from the user
   @IBAction func addToMap(_ sender: Any) {
      let lat = latitudeField.text!
      let long = longitudeField.text!
      let title = titleTextField.text!
      var location: [String: Any] = ["title": title, "latitude": lat, "longitude": long]
      locations.append(location) // adding the info to the array
      let mapVC : MapViewController = MapViewController()
      mapVC.iterateLocations()
  
      }
  
  
  }
  </code></pre>
  
  <p>MapView class file:</p>
  
  <pre><code>import UIKit
  import MapKit
  class MapViewController: UIViewController, MKMapViewDelegate {
  
  
      @IBOutlet weak var mapView: MKMapView!
  
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
          mapView.delegate = self
      }
  
      // this method here iterates through each of the locations in the array
      func iterateLocations() -&gt; Bool {
          for location in locations {
  
              var momentaryLat = (location["latitude"] as! NSString).doubleValue
              var momentaryLong = (location["longitude"] as! NSString).doubleValue
              let annotation = MKPointAnnotation()
              annotation.title = location["title"] as? String
              annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
              mapView.addAnnotation(annotation)
          }
          return true
      }
  
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -&gt; MKAnnotationView? {
          let identifier = "pinAnnotation"
          var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
  
          if annotationView == nil {
               annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
               annotationView?.canShowCallout = true
          }
  
          annotationView?.annotation = annotation
          return annotationView
          }
  }
  </code></pre>
  
  <p>The error pops up in the line "mapView.addAnnotation(annotation)" with it saying that a nil was found when trying to unwrap an Optional. So I think the error is that the information is not saving in annotation, but I don't immediately see where this is wrong. </p>
  
  <p>Any alternatives for this method, that may be easier to implement, are welcome!</p>