如何在不出错的情况下执行以下操作?专栏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
属性所属的类的实例。
答案 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
}()