委托在类中调用后返回nil

时间:2019-03-24 16:00:04

标签: swift delegates location-services

我正在使用此类来获取位置,并将其与委托一起发送到另一个视图控制器,但是委托在调用时返回nil。 我的班级:

protocol LocationUpdateProtocol {
    func locationDidUpdateToLocation(location : CLLocation)
}
/// Notification on update of location. UserInfo contains CLLocation for key "location"
let kLocationDidChangeNotification = "LocationDidChangeNotification"
class LocationServiceHelper:NSObject,CLLocationManagerDelegate {

    static let sharedManager = LocationServiceHelper()
    private let locationManager = CLLocationManager()
    var currentLocation:CLLocation?
    weak var delegate :LocationUpdateProtocol?
    private override init() {
        super.init()
        self.locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    // MARK: CLLocationManagerDelegate

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let delegate = self.delegate else {
            return
        }
        currentLocation = locations.first
        let userInfo:NSDictionary = ["location":currentLocation!]
        delegate.locationDidUpdateToLocation(location: currentLocation!)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kLocationDidChangeNotification), object: self, userInfo: userInfo as? [AnyHashable : Any])
    }
}

我在下面的另一个班级呼叫了代表:

var locationServiceReference = LocationServiceHelper.sharedManager
    override func viewDidLoad() {
        super.viewDidLoad()
        fillFields()
      locationServiceReference.delegate = self
        self.mapView.showsUserLocation = true
    }

func locationDidUpdateToLocation(location: CLLocation) {
       // print(location)
        // let myLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let myLocationForTesting = CLLocationCoordinate2D(latitude: 33.410165, longitude: 35.480060)
        myLocation = location
            let region = MKCoordinateRegion(center: myLocationForTesting, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        self.mapView.setRegion(region, animated: true)

    }

在did update位置,委托返回nil,知道发生了什么吗?该函数未被调用,因为委托返回nil。

2 个答案:

答案 0 :(得分:0)

  1. 确保您的控制器仍在内存中
  2. 您不需要使用防护装置。可选链接在以下情况下效果更好:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    currentLocation = locations.first
    let userInfo:NSDictionary = ["location":currentLocation!]
    delegate?.locationDidUpdateToLocation(location: currentLocation!)
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: kLocationDidChangeNotification), object: self, userInfo: userInfo as? [AnyHashable : Any])
}
  1. 确保在设置代表后调用func locationManager(_:didUpdateLocations)
  2. 将来,当代理人更改为时,您希望传递上一个位置:
weak var delegate :LocationUpdateProtocol? {
    didSet { 
        guard let last = currentLocation else { return }
        delegate?.locationDidUpdateToLocation(location: last)
    }
}
  1. 由于它是一个只有1个委托人的单例,因此请确保不要将委托人设置为nil或其他地方的短暂居住对象。

答案 1 :(得分:0)

此代码-

var locationServiceReference = LocationServiceHelper.sharedManager

在设置delegate之前执行,这就是delegate总是返回nil的原因。

替换

var locationServiceReference = LocationServiceHelper.sharedManager

使用

var locationServiceReference : LocationServiceHelper!

并在调用viewDidLoad()之后初始化。

override func viewDidLoad() {
    super.viewDidLoad()

    locationServiceReference = LocationServiceHelper.sharedManager
    locationServiceReference.delegate = self

}

希望它会帮助你。让我知道您是否仍然有任何问题。