'Unrecognized selector sent to instance.. 'NSInvalidArgumentException' for MapView delegate
Swift 4/Google Maps.
The crash I get is..
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setDelegate:]: unrecognized selector sent to instance 0x7fa1eb50d7e0'
Yes, I know there are a lot of threads on this, but looking through them has not helped me. I have debugged enough where I have a suspect, but do not know exactly how to resolve.
Some detail.. The MapViewController has three UIViews.
- MapViewController
- Main View (default one that comes with the controller) -- Menu (sub view of Main View) -- Map (sub view of Main View)
I believe I've narrowed down the crash to the statement
self.mapView.delegate = self
Here's most of the code for the MapViewController.
import UIKit
import GoogleMaps
import FirebaseDatabase
class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var mapView: GMSMapView!
var locationOfInterestMarker = GMSMarker()
var userMarker = GMSMarker()
var locationManager = CLLocationManager()
let zoom:Float = 15
let locationOfInterestImage:String = "hhouseicon"
let userMarkerImage:String = "witchicon"
override func viewDidLoad() {
super.viewDidLoad()
self.getLocations()
self.locationManager.delegate = self
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startMonitoringSignificantLocationChanges()
/**
The next line causes crash. I believe I need to set the delegate to something other than self. Set the delegate to the View named Map. However I'm not sure about the actual code for that.
**/
self.mapView.delegate = self //Crashes
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
self.locationManager.startUpdatingLocation()
self.mapView.isMyLocationEnabled = true
self.mapView.settings.myLocationButton = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
self.locationManager.stopUpdatingLocation()
self.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
self.placeMarker(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude, marker: self.userMarker, imageName: userMarkerImage)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if let destination = segue.destination as? LocationDetialViewController {
destination.coordinate = locationManager.location?.coordinate
} else {
return
}
} . . .
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
performSegue(withIdentifier: "locationDetailSegue", sender: self)
return false
}
}//MapViewController
Solution 1:[1]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luis Becerra |

