0

I'm trying to request permission to get someones location but the app crashes because of a connection I have.

Other people have suggested to check any outlets that aren't connected. I only have one and I have deleted it and reconnected it multiple times. I don't believe that is the problem but I may be missing something. I think the problem is something else to do with the fact that I was following an old tutorial.

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7feb27510490> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainMapView.'

This is my code:

func checkLocationAuthorization() {
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        //do map stuff
        break
    case .denied:
        // show alert to turn on permissions
        break
    case .notDetermined:
        // havent picked yet
        locationManager.requestWhenInUseAuthorization()
    case .restricted:
        //show alert
        break
    case .authorizedAlways:
        break
    }
}

I'm also getting an alert on the switch; saying it may have additional unknown values. that might be the issue

Rob
  • 415,655
  • 72
  • 787
  • 1,044
alexander
  • 9
  • 3
  • "Other people have suggested to check any outlets that aren't connected. I only have one and I have deleted it and reconnected it multiple times" The class of the `UIViewController` in your Storyboard is `UIViewController` (default one) and not the custom one you use. It could be because you didn't set it, or a case to check in Modules/Targets. – Larme Apr 15 '19 at 16:27
  • Add your xib or Storyboard screen – Toseef Khilji Apr 15 '19 at 16:30

2 Answers2

0

You report that you are receiving an exception:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7feb27510490> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainMapView.'

The presence of UIViewController in that exception is telling you that this base class doesn’t have that outlet. Note it’s not referencing your class, but rather UIViewController. That suggests that you neglected to set the base class for your scene in the storyboard.


If your question is about this code in Swift 5, you will receive a compile-time warning and you can tap on that alert and then on its “fix” button, it will add the necessary clause (I’ve replaced the default fatalError() with break):

 func checkLocationAuthorization() {
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        //do map stuff
        break

    case .denied:
        // show alert to turn on permissions
        break

    case .notDetermined:
        // havent picked yet
        locationManager.requestWhenInUseAuthorization()

    case .restricted:
        //show alert
        break

    case .authorizedAlways:
        break

    @unknown default:
        break
    }
}

The idea in Swift 5 is that we should also add a @unknown clause for any cases that might be added in future iOS versions in order to future-proof our code. (If you already have a default clause, though, this is not needed; only when you’ve enumerated all of the clauses do you need to add this @unknown case.) See SE-0192: Handling Future Enum Cases.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • When I click on the view controller in the storyboard and look at the identity inspector, it says my class is called "ViewController". Does that mean I need to change it to "UIViewController"? I don't understand – alexander Apr 16 '19 at 13:12
  • If your exception is saying “'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainMapView.'”, (with `UIViewController`, not `ViewController` or whatever) then that means that it has instantiated a `UIViewController`, not your subclass. The typical source of that problem is the failure to specify one’s base class correctly. But it sounds like you have set it to `ViewController`, so it’s not immediately obvious why you’re having that problem. I’d suggest creating a new blank project and seeing if you can reproduce the problem there. – Rob Apr 16 '19 at 15:28
-1

Check your IBOutlet from storyboard. Probably smth wrong with your mainMapView.

Anton
  • 111
  • 10