Search code examples
iosswiftcllocationmanagercllocation

"Value of type '(CLLocationManager, [CLLocation]) -> ()' has no member 'delegate' " Error in swift with mapping


When trying to find the location of a user, I use CLLocationManager. Where I have this line:

locationManager.delegate = self

It returns this error:

Value of type '(CLLocationManager, [CLLocation]) -> ()' has no member ‘delegate'

Here is the code:

import CoreLocation
import UIKit
import SwiftUI
import Mapbox

class MapsViewController: UIViewController, MGLMapViewDelegate {
    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled(){
            locationManager.delegate = self //this is where I get the error said above
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
}

I have tried the error on stackoverflow and solving it myself, but nothing has helped so far. It might be a problem with the new version of Swift & Xcode (as I am following this tutorial)

I add Privacy - Location When In Use Usage Description and Privacy - Location Always and When In Use Usage Description in Info.plist for anyone wondering.


Solution

  • Add CLLocationManagerDelegate

    class MapsViewController: UIViewController,MGLMapViewDelegate,CLLocationManagerDelegate{