0

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:

enter image description here

Daniel C.
  • 151
  • 1
  • 2
  • 10
  • What's the full error message? – dan May 26 '17 at 15:22
  • Does it crash in this line? performSegue(withIdentifier: "vista", sender: self) – ff10 May 26 '17 at 15:26
  • @dan I will take a pic of the error and edit the question. – Daniel C. May 26 '17 at 15:31
  • @ff10 When I get the error it shows it in the AppDelegate.swift – Daniel C. May 26 '17 at 15:31
  • If I remove the function "override func prepare(for segue: UIStoryboardSegue, sender: Any?) " when I click the callout, the error appears at AppDelegate. – Daniel C. May 26 '17 at 15:34
  • Which line does your code crash at? What is your `DetailView` class? You are trying to cast the destination view controller to a `ViewController` class. `ViewController` should be the name of the `UIViewController` subclass you are segueing to. FYI: `ViewController` is a a bad name for a class - the class name should be more descriptive. For instance `MapViewController`. – Robotic Cat May 26 '17 at 15:36
  • @RoboticCat it crashes in the AppDelegate, in this line (class AppDelegate: UIResponder, UIApplicationDelegate {). DatailClass is empty (only defaults functions when created). What can I do :S – Daniel C. May 26 '17 at 15:40
  • Possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"](https://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v) – dan May 26 '17 at 15:47
  • 1
    It is very unlikely to be crashing in the AppDelegate. Add an Exception Breakpoint (`Step 1`). Also use the debugger and breakpoints to step through your code (`Step 2`). Finally, if the error is in the segue then the problem is probably as I suggested - you want to segue to the `UIViewController` subclass on the other end of the segue that you have set in IB (Is this `DetailClass`? - another bad name. Is this a View Controller?). Currently you are trying to present a `ViewController` class when I suspect you want to present a `DetailClass`. – Robotic Cat May 26 '17 at 15:47
  • my DetailClass is a subclass of UIViewController, so maybe as you said, the segue might be the problem. – Daniel C. May 26 '17 at 15:52

0 Answers0