Home>

Swift beginner.
I would like to create a trajectory program that uses Swift and Mapkit to connect my current location with pins.
And I want to be able to use the simulator Freeway drive.
Thread 1: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0)
The message appears, and the app crashes although it can be compiled.
I would like you to tell me.
Thank you in advance.

Error message
Error message
fatal error: unexpectedly found nil while unwrapping an Optional value
Applicable source code
// Method from the beginning
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Set yourself as the delegate destination.
        testManager.delegate = self
        // Start acquiring location information.
        testManager.startUpdatingLocation ()
        // Pop up a screen to change permission to use location information.
        testManager.requestWhenInUseAuthorization ()
        // Set the delegate destination to yourself.
        testMapView.delegate = self
        // ↑ error message here
    }

    // Call method for obtaining location information
    func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        for location in locations {
            // center coordinates
            let center = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
            //Display range
            let span = MKCoordinateSpanMake (0.01, 0.01)
            // Register center coordinates and display range in the map.
            let region = MKCoordinateRegionMake (center, span)
            testMapView.setRegion (region, animated: true)
            if (annotation == nil) {
                // The first time stores the pin in the map.
                annotation = MKPointAnnotation ()
                annotation.coordinate = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
                testMapView.addAnnotation (annotation)
            } else {
                // 2 After that, draw a straight line between the coordinates before and after the movement.
                // Start and end coordinates
                var lineLocation: [CLLocationCoordinate2D] = [CLLocationCoordinate2D (latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude),
                                                             CLLocationCoordinate2D (latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
                // 2 Draw a straight line between points.
                let line = MKPolyline (coordinates:&lineLocation, count: 2)
                testMapView.add (line)
                // Update the pin position.
                annotation.coordinate = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
            }
        }
    }

Please describe what you tried for the problem here.

Supplemental information (FW/tool version etc.)

Swift 3.0.2
Xcode 8.2.1

Describe the entire code.

import UIKit
import MapKitclass ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    @IBOutlet weak var testMapView: MKMapView!
    var testManager: CLLocationManager = CLLocationManager ()
    // Annotation
    var annotation: MKPointAnnotation!
    // A method from the beginning
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Set yourself as the delegate destination.
        testManager.delegate = self
        // Start acquiring location information.
        testManager.startUpdatingLocation ()
        // Pop up a screen to change permission to use location information.
        testManager.requestWhenInUseAuthorization ()
        // Set the delegate destination to yourself.
        testMapView.delegate = self
    }

    // Call method for obtaining location information
    func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        for location in locations {
            // center coordinates
            let center = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
            //Display range
            let span = MKCoordinateSpanMake (0.01, 0.01)
            // Register center coordinates and display range in the map.
            let region = MKCoordinateRegionMake (center, span)
            testMapView.setRegion (region, animated: true)
            if (annotation == nil) {
                // The first time stores the pin in the map.
                annotation = MKPointAnnotation ()
                annotation.coordinate = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
                testMapView.addAnnotation (annotation)
            } else {
                // 2 After that, draw a straight line between the coordinates before and after the movement.
                // Start and end coordinates
                var lineLocation: [CLLocationCoordinate2D] = [CLLocationCoordinate2D (latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude),
                                                             CLLocationCoordinate2D (latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
                // 2 Draw a straight line between points.
                let line = MKPolyline (coordinates:&lineLocation, count: 2)
                testMapView.add (line)
                // Update the pin position.
                annotation.coordinate = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
            }
        }
    }

    // Call method when drawing method is executed
    func mapView (_ mapView: MKMapView, rendererFor overlay: MKOverlay)->MKOverlayRenderer {
        let testRender = MKPolylineRenderer (overlay: overlay)
        // Set the line width.
        testRender.lineWidth = 3
        // Set the line color.
        testRender.strokeColor = UIColor.red
        return testRender
    }
}
Tried
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {@IBOutlet weak var testMapView: MKMapView?
    var testManager: CLLocationManager = CLLocationManager ()
    // Annotation
    var annotation: MKPointAnnotation!

    // A method from the beginning
    override func viewDidLoad () {
        super.viewDidLoad ()

        // Set yourself as the delegate destination.
        testManager.delegate = self
        // Start acquiring location information.
        testManager.startUpdatingLocation ()
        // Pop up a screen to change permission to use location information.
        testManager.requestWhenInUseAuthorization ()
        print ("testMapView:", testMapView)
        // Set the delegate destination to yourself.
        testMapView? .delegate = self
    }

    // Call method for obtaining location information
    func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        for location in locations {
            // center coordinates
            let center = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
            //Display range
            let span = MKCoordinateSpanMake (0.01, 0.01)
            // Register center coordinates and display range in the map.
            let region = MKCoordinateRegionMake (center, span)
            testMapView? .setRegion (region, animated: true)
            if (annotation == nil) {
                // The first time stores the pin in the map.
                annotation = MKPointAnnotation ()
                annotation.coordinate = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
                testMapView? .addAnnotation (annotation)
            } else {
                // 2 After that, draw a straight line between the coordinates before and after the movement.
                // Start and end coordinates
                var lineLocation: [CLLocationCoordinate2D] = [CLLocationCoordinate2D (latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude),
                                                             CLLocationCoordinate2D (latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
                // 2 Draw a straight line between points.
                let line = MKPolyline (coordinates:&lineLocation, count: 2)
                testMapView? .add (line)
                // Update the pin position.
                annotation.coordinate = CLLocationCoordinate2DMake (location.coordinate.latitude, location.coordinate.longitude)
            }
        }
    }

    // Call method when drawing method is executed
    func mapView (_ mapView: MKMapView, rendererFor overlay: MKOverlay)->MKOverlayRenderer {
        let testRender = MKPolylineRenderer (overlay: overlay)
        // Set the line width.
        testRender.lineWidth = 3
        // Set the line color.
        testRender.strokeColor = UIColor.red
        return testRender
    }
}