I am working on an App where you have access to a Map and you can add some annotations and when you click them a callout appears with a button and if you press it, it will send you to another view. The problem is, when I want to open the View, the App crashes and I see the error I mentioned in the title. I don't know if this is a problem with my Userdefaults or maybe a problem with a function when I prepare a segue to call the view. I appreciate your help and comments. Here is my code...
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var hospitales = [MKPointAnnotation]()
var imagenes = [UIImage]()
//For UserDafaults data
var nombreHospital = [String]()
var distanciaEntreUbicaciones = [Double]()
struct Location {
let title: String
let latitude: Double
let longitude: Double
}
let locations = [
Location(title: "Hospital Zambrano", latitude: 25.647399800, longitude: -100.334304500),
Location(title: "Christus Mugerza Sur", latitude: 25.589339000, longitude: -100.257724800),
Location(title: "Saint Paul Hospital", latitude: 49.280524700, longitude: -123.128232600)
]
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.showsUserLocation = true
for location in locations {
//var i = 0
let point = MKPointAnnotation()
point.title = location.title
point.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude)
nombreHospital.append(location.title)
//i += 1
//mapView.addAnnotation(point)
if let currentLocation = locationManager.location?.coordinate {
let locationMapPoint = MKMapPointForCoordinate(currentLocation)
let pinMapPoint = MKMapPointForCoordinate(point.coordinate)
let distance = MKMetersBetweenMapPoints(locationMapPoint, pinMapPoint)
UserDefaults.standard.set(distanciaEntreUbicaciones, forKey: "distancia")
UserDefaults.standard.synchronize()
if distance >= 0 && distance <= 45000000 {
let distancia: Double = round (distance / 1000)
distanciaEntreUbicaciones.append(distancia)
point.subtitle = "Dist. \(distancia) kilometros"
mapView.addAnnotation(point)
//Does not perform condition... Or that´s what happens to me
}
}
}
UserDefaults.standard.set(nombreHospital, forKey: "nombre")
UserDefaults.standard.set(distanciaEntreUbicaciones, forKey: "distancia")
UserDefaults.standard.synchronize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKPointAnnotation) {
print("No es de tipo MKPointAnnotation")
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
if annotationView == nil{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
annotationView!.canShowCallout = true
annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
else {
annotationView!.annotation = annotation
}
annotationView!.image = UIImage(named: "curz")
return annotationView
}
var anotacionSeleccionada : MKPointAnnotation!
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
anotacionSeleccionada = view.annotation as? MKPointAnnotation
performSegue(withIdentifier: "vista", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//I want to open the segue in my DetailView class is it ok
//if I leave the as? ViewController? or should I change it?
if let destination = segue.destination as? ViewController {
//There´s also an error here, I want to open the view for an
//specific annotation, maybe an array will be good
destination.hospitales[] = anotacionSeleccionada
}
}
}
The error: