0

I am attempting to show multiple objects on my map annotations from Care Data. Currently, I can get title and subtitle to show info with this function

 func getData() -> [MKAnnotation]? {





        do {
            storedLocations = try context.fetch(Fish.fetchRequest())
            var annotations = [MKAnnotation]()
            for storedLocation in storedLocations {
                let newAnnotation = MyAnnotation(coordinate: CLLocationCoordinate2D(latitude: storedLocation.latitude, longitude: storedLocation.longitude))
                newAnnotation.coordinate.latitude = storedLocation.latitude
                newAnnotation.coordinate.longitude = storedLocation.longitude
                newAnnotation.title = storedLocation.species
                newAnnotation.subtitle = storedLocation.length
                newAnnotation.newTime = storedLocation.time
                newAnnotation.newBait = storedLocation.bait
                newAnnotation.newNotes = storedLocation.notes
                newAnnotation.newWaterTempDepth = storedLocation.water
                newAnnotation.newWeatherCond = storedLocation.weather




              // print(storedLocation.length)
                let newLength = storedLocation.length
                let newTime = newAnnotation.newTime
//                let newBait = storedLocation.bait
//                let newNotes = storedLocation.notes
//                let newWaterTempDepth = storedLocation.water
//                let newWeatherCond = weatherCond

                myLength = newLength!
                myTime = newTime!

//

                annotations.append(newAnnotation)





            }

            return annotations
        }
        catch {
            print("Fetching Failed")
        }
        return nil
    }

I am trying to show all of the storedLocation data on the annotation.

By using this extension I found here I am able to add multiple lines to the annotation. However it is not from the info stored in core data. This is how I set up the func for the extension

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }

        let reuseIdentifier = "pin"
        var annotationView = map.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)


        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            annotationView?.canShowCallout = true

            annotationView?.loadCustomLines(customLines: [myLength, myTime])


        } else {
            annotationView?.annotation = annotation
        }


        annotationView?.image = UIImage(named: "fish")


         return annotationView

Here is the extension.

 extension MKAnnotationView {

    func loadCustomLines(customLines: [String]) {
        let stackView = self.stackView()
        for line in customLines {
            let label = UILabel()
            label.text = line
            stackView.addArrangedSubview(label)
        }
        self.detailCalloutAccessoryView = stackView
    }



    private func stackView() -> UIStackView {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        return stackView
    }
}

here is my annotation

    import UIKit
    import MapKit

    class MyAnnotation: NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    var newLength: String?
    var newTime: String?
    var newBait: String?
    var newNotes: String?
    var newWaterTempDepth: String?
    var newWeatherCond: String?
    var detailCalloutAccessoryView: UIStackView?



      init(coordinate: CLLocationCoordinate2D){
        self.coordinate = coordinate
    }


   }

When I run I get this

I am very new to programming and would greatly appreciate any help, and also an explanation of what I am doing wrong Thank you in advance

jesseCampeez
  • 97
  • 10

1 Answers1

0

'!' means Force Unwrapping

myLength = newLength! <- CRASH

As 'newLength' is nil here, you will get a crash on the line where you force unwrap it.

e.g. You can fix the error

myLength = newLength ?? 0
myTime = newTime ?? 0

See also What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • this reading did help me to understand Optionals much better. However it also created more errors than I started with. I have been stuck on this for over a week and feel completely lost. – jesseCampeez Jul 17 '18 at 16:01
  • I re-read the link and finally realized my error! I had not set var myLength: String = "" , to the optional var myLength: String? = "" This and some major cleanup after what I learned it is now working great! – jesseCampeez Jul 17 '18 at 16:20