2

I am very new to stackexchange so please let me know if i need to provide more information.

I tried to install the Corelocation and Mapkit and use them togheter. Every since i followed Vea Software Tutorial i have recived the following error:

Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)

In the compiler it says

fatal error: Unexpectedly found nil while unwrapping an Optional Value(llDB)

The error is appering right at this line, but when i delete it, it appears at another Mapkit/Location core line. The error:

self.mapView.showsUserLocation = true

EDIT: When i remove the line above, the same error appears on

 self.mapView.setRegion(region, animated: true)

I have been searching around for a while on the internet but nothing really helps me out. Thank you for your time.

If you need the whole viewcontroller code. Here it is.

import UIKit
import Parse
import CoreLocation
import MapKit  


class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()


    @IBOutlet var UsernameTextField: UITextField!
    @IBOutlet var PasswordTF: UITextField!
    @IBOutlet var EmailTF: UITextField!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    self.locationManager.delegate = self

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.startUpdatingLocation()

    self.mapView.showsUserLocation = true

    }






    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: - Location Delegate Methods

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       let location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

        self.mapView.setRegion(region, animated: true)

        self.locationManager.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error:" + error.localizedDescription)
    }

}
Tom James
  • 59
  • 10
  • al those variables with an exclamation mark are being forced unwrapped so any of those could be crashing your app with that error. Also in your code I see `let location = locations.last` and then you use `location!`. Any chance location is nil? – tmpz Jan 11 '16 at 20:38
  • I tried going in to Debug -> Simulate Location, picked a randome location and than tried to run it. Still getting the same error – Tom James Jan 11 '16 at 20:47
  • That doesn't really answer my question my I will take it as a no. What error message is telling is that any of those optionals `@IBOutlet weak var mapView: MKMapView! @IBOutlet var UsernameTextField: UITextField! @IBOutlet var PasswordTF: UITextField! @IBOutlet var EmailTF: UITextField! let location = locations.last` is .None (nil) instead of being .Some so my advice would be to check those. Maybe if your xib they are referencing something that is not there anymore – tmpz Jan 11 '16 at 20:50
  • I am sorry. I thought i had removed those from the script to keep it relevant. Those code worked fine before i tried to add the Mapkit and Corelocation. – Tom James Jan 11 '16 at 20:53
  • Also I just noticed your variable names are uppercase for most of the outlets. Don't know if you intended that. – tmpz Jan 11 '16 at 20:53
  • I did intended that. I have also went over the outlets and everything seems to be right. I cant figure out where the problem is... – Tom James Jan 11 '16 at 20:58
  • My advice is to put a breakpoint in your delegate method `locationManager(manager: ... ` The problem is more likely there. – tmpz Jan 11 '16 at 20:58
  • Wen i remove sef.mapView.showsUserlocation = true the excact same error appears on another .mapView. may the problem be with the connection with the main.viewcontroller and map? – Tom James Jan 11 '16 at 21:02
  • okei, i will try that as well. Thank you – Tom James Jan 11 '16 at 21:02
  • It's had for me to know what you view controller set up looks like but as you just said when you remove the line the problem appears somewhere else. So most probably `self.mapView` is nill. In fact you have declared it as forced unwrapped. For a test try to remove the `weak` specifier. – tmpz Jan 11 '16 at 21:05
  • Also try to declare self.mapView as @IBOutlet weak var mapView: MKMapView? – tmpz Jan 11 '16 at 21:08
  • I am very new to SWIFT and coding. I will try to declare it. I got a error on that while coding. I will try to fix it. Sorry adn thank you for you time! I really appriciate it. – Tom James Jan 11 '16 at 21:14

1 Answers1

1

This just an hypothesis by I noticed you use

self.mapView.setRegion(region, animated: true)

in the CLLocationManager delegate and start updating before mapView is actually showing (i.e. in viewDidLoad).

Could it be that mapView is trying to update its visual states before being installed in the view hierarchy ?

Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Sorry, i did not quite understand what you ment. I tried to moce the self.mapView.setRegion before i update location. Did not work. When i remove the self.mapView.showsUserlocation (where the error is), the error comes up at another .mapView. (the self.mapView.setRegion) – Tom James Jan 12 '16 at 09:56
  • I can't test this but from similar experience using other components, I would suggest you move self.locationManager.startUpdatingLocation() and self.mapView.showsUserLocation = true to a function that is executed after the view hierarchy is ready. e.g. viewWillAppear or viewDidAppear – Alain T. Jan 12 '16 at 13:30
  • When i put the "self.mapView.showsUserLocation = true" Under " func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {" The app runs fine without errors, but it wont look for my location – Tom James Jan 12 '16 at 14:14
  • Did you try what I suggested ? i.e. moving self.locationManager.startUpdatingLocation() and self.mapView.showsUserLocation = true to viewDidAppear() – Alain T. Jan 13 '16 at 05:41