【发布时间】:2021-03-07 18:51:04
【问题描述】:
我正在使用以下可观察对象来跟踪用户位置:
import Foundation
import MapKit
class LocationManager: NSObject, ObservableObject {
private let locationManager = CLLocationManager()
@Published var location: CLLocation? = nil
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationServicesEnabled() -> Bool {
return self.locationManager.locationServicesEnabled()
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
self.location = location
}
}
但是,当我尝试编译时,return self.locationManager.locationServicesEnabled() 语句旁边出现错误。编译器说:Static member 'locationServicesEnabled' cannot be used on instance of type 'CLLocationManager', Replace 'self.locationManager' with 'CLLocationManager'。我不明白这个错误,因为self.locationManager 是CLLocationManager 的一个实例。请帮忙。
【问题讨论】:
标签: swift xcode mapkit cllocationmanager