title: Swift app crashes with `unrecognized selector sent to instance` on CLLocationManager
body: <p>Similar to many, many other questions on StackOverflow, I get a <code>unrecognized selector sent to instance</code> fault, when running my app. What makes my case different (I think) is that it is written in Swift and that the common reason is not the reason here.</p>
  
  <p>In <a href="https://stackoverflow.com/questions/24169390/xcode6-swift-unrecognized-selector-sent-to-instance">this</a> question the solution is to set the "Custom Class" in the Identity Inspector for that view to the responding class. I checked this and this is the case (note that I did rename the class at some point, but it now is definitely set to "ViewController").</p>
  
  <p>I include the stack trace below as well as the code for <code>ViewController.swift</code>. You can also find all of the code <a href="https://github.com/unapiedra/Swift-Map-Display/commit/e16c414717f1d86c9911528be389ab539cc503b8" rel="nofollow noreferrer">here</a>. I managed to avoid this problem by starting fresh, which enabled me to solve my <a href="https://stackoverflow.com/questions/24497928/ios-app-doesnt-ask-for-location-permission">earlier question</a>. This new trial can be found in <a href="https://github.com/unapiedra/Swift-Map-Display/commit/acb074692f8b2ed29e091b5a3f2416bbad2b9086" rel="nofollow noreferrer">this branch</a>. But, I want to know what is wrong and solve this.</p>
  
  <h2>Some thought</h2>
  
  <p>One question that I couldn't answer successfully is whether <code>CLLocationManagerDelegate</code> needs some required methods? But because I got it to work in the other branch, I suspect it's not necessary.</p>
  
  <pre><code>2014-07-03 21:50:26.056 RowingTracker2[11416:60b] -[CLLocationManager requestWhenInUseAuthorization]: unrecognized selector sent to instance 0xa10c4a0
  2014-07-03 21:50:26.059 RowingTracker2[11416:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocationManager     requestWhenInUseAuthorization]: unrecognized selector sent to instance 0xa10c4a0'
  *** First throw call stack:
  (
      0   CoreFoundation                      0x008831e4 __exceptionPreprocess + 180
  1   libobjc.A.dylib                     0x01e2a8e5 objc_exception_throw + 44
  2   CoreFoundation                      0x00920243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
  3   CoreFoundation                      0x0087350b ___forwarding___ + 1019
  4   CoreFoundation                      0x008730ee _CF_forwarding_prep_0 + 14
  5   RowingTracker2                      0x00003352 _TFC14RowingTracker214ViewController11viewDidLoadfS0_FT_T_ + 738
  6   RowingTracker2                      0x000033a2 _TToFC14RowingTracker214ViewController11viewDidLoadfS0_FT_T_ + 34
  7   UIKit                               0x0102f33d -[UIViewController loadViewIfRequired] + 696
  8   UIKit                               0x0102f5d9 -[UIViewController view] + 35
  9   UIKit                               0x00f4f267 -[UIWindow addRootViewControllerViewIfPossible] + 66
  10  UIKit                               0x00f4f5ef -[UIWindow _setHidden:forced:] + 312
  11  UIKit                               0x00f4f86b -[UIWindow _orderFrontWithoutMakingKey] + 49
  12  UIKit                               0x00f5a3c8 -[UIWindow makeKeyAndVisible] + 65
  13  UIKit                               0x00f0abc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
  14  UIKit                               0x00f0f667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
  15  UIKit                               0x00f23f92 -[UIApplication handleEvent:withNewEvent:] + 3517
  16  UIKit                               0x00f24555 -[UIApplication sendEvent:] + 85
  17  UIKit                               0x00f11250 _UIApplicationHandleEvent + 683
  18  GraphicsServices                    0x031fff02 _PurpleEventCallback + 776
  19  GraphicsServices                    0x031ffa0d PurpleEventCallback + 46
  20  CoreFoundation                      0x007feca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
  21  CoreFoundation                      0x007fe9db __CFRunLoopDoSource1 + 523
  22  CoreFoundation                      0x0082968c __CFRunLoopRun + 2156
  23  CoreFoundation                      0x008289d3 CFRunLoopRunSpecific + 467
  24  CoreFoundation                      0x008287eb CFRunLoopRunInMode + 123
  25  UIKit                               0x00f0ed9c -[UIApplication _run] + 840
  26  UIKit                               0x00f10f9b UIApplicationMain + 1225
  27  RowingTracker2                      0x00008021 top_level_code + 97
  28  RowingTracker2                      0x0000805b main + 43
  29  libdyld.dylib                       0x024ff701 start + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException
  (lldb) 
  </code></pre>
  
  <p>ViewController.swift</p>
  
  <pre><code>import UIKit
  import MapKit
  import CoreLocation
  
  class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
      @IBOutlet var mapview: MKMapView = nil
      var locationmgr : CLLocationManager!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          locationmgr = CLLocationManager()
          locationmgr.delegate = self
          locationmgr.requestWhenInUseAuthorization() ///&lt; Offending line.
  //        mapview.showsUserLocation = true
      }
  
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  }
  </code></pre>