Value of type '...' has no member '...' errors

Hello


I am creating an app that uses ibeacons and am close to finishing it however I have two errors that I don't know how to fix and cannot find a solution for anywhere.


The code are asociated with the following lines:


locationManager.startMonitoring(for: beaconRegion)

Value of type '(CLLocationManager, [CLBeacon], CLBeaconRegion) -> has no member 'startMonitoring'


and


locationManager.startRangingBeacons(in: beaconRegion)

Value of type '(CLLocationManager, [CLBeacon], CLBeaconRegion) -> has no member 'startRangingBeacons'


Here is the entire section of code that I believe is contributing to this error:

        let uuid = UUID(uuidString: "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0")!
        let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "EVITA")
       
        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(in: beaconRegion)
    }
   
    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        if beacons.count > 0 {
            updateDistance(beacons[0].proximity)
        } else {
            updateDistance(.unknown)
        }
    }


Can anyone explain and potentially provide me a solution so that I can fix these errors, they are the only two errors that I have.


I am using Xcode 8.3.2 and am happy to post my entire code if you need it.


Thank you

How have you declared the property (or local variable?) `locationManager`?

By that do you mean the lines:

var locationManager: CLLocationManager!
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()


I'm sorry I'm new so I'm not too sure what you mean, I have been following this tutorial:

https://www.hackingwithswift.com/example-code/location/how-to-detect-ibeacons

Seems compiler is confused between a var locationManager (that it does not see, but should be declared as

var locationManager: CLLocationManager

and the func locationManager().


I personnaly find it better to use different names for func and var or properties, but that's a personal preference.

What do you mean by this? What should I change the names to that in your experience works best?

I just mean it is to avoid the problem you face.


if you call your var userLocationManager, then you will see if the var is visible at the place you try to call it.


That will not solve the problem but help find its cause.

I cannot reproduce the same error you have shown with all lines put in a single `

ViewController
` class.

Have you tried to build your project ignoring the errors?

(Sometimes Xcode editor continues to show some old, old erros...)


If you get these two errors on build time, you may need to show your whole code.

Did you clean the build folder ? (option clean)

Looking at the tutorial, my guess is you've mistakenly declared var locationManager as a local variable in method viewDidLoad instead of as an instance property of class ViewController. Your code should look similar to:


class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
    }

    ...
}


(If locationManager is declared as a local variable inside viewDidLoad() instead of as an instance property of ViewController, then it will not exist outside of viewDidLoad(). In that case, and when ViewController has a method also named locationManager, the compiler will infer that locationManager used outside of viewDidLoad must be referring to the method with that name, whereas, at the site of the reported error, you were intending locationManager to refer to a property with that name.)

Value of type '...' has no member '...' errors
 
 
Q