在创建CLLocationManager时分配CLLocationManager委托

时间:2017-11-06 10:36:56

标签: ios swift variables

如何在不出错的情况下执行以下操作?专栏locationManager.delegate = self表示不能Cannot assign value of type '(APPLocationManager) -> () -> (APPLocationManager)' to type 'CLLocationManagerDelegate?'

let locationManager: CLLocationManager = {
    let locationManager = CLLocationManager()

    locationManager.delegate = self
    locationManager.activityType = CLActivityType.other
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.pausesLocationUpdatesAutomatically = true
    locationManager.distanceFilter = APPLocationManagerDistanceFilter

    return locationManager
}()

我希望委托是locationManager属性所属的类的实例。

1 个答案:

答案 0 :(得分:1)

在开始任何服务之前,您应该将委托对象分配给CLLocationManager对象的委托属性。 as -

class ViewController: UIViewController, CLLocationManagerDelegate{
lazy var locationManager: CLLocationManager = {
        let locationManager = CLLocationManager()

        locationManager.delegate = self
        locationManager.activityType = CLActivityType.other
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = true
        locationManager.distanceFilter = APPLocationManagerDistanceFilter

        return locationManager
    }()