swift - 委托在类中调用后返回 nil
问题描述
我正在使用此类来获取位置并将其与委托一起发送到另一个视图控制器,但委托在调用时返回 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)
}
在更新位置中,代表返回 nil,知道发生了什么吗?该函数未被调用,因为委托返回 nil。
解决方案
- 确保您的控制器仍在内存中
- 你不需要使用守卫。可选链在这种情况下效果更好:
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])
}
- 确保
func locationManager(_:didUpdateLocations)
在委托设置后调用。 - 对于将来,您希望在委托更改为时传递最后一个位置:
weak var delegate :LocationUpdateProtocol? {
didSet {
guard let last = currentLocation else { return }
delegate?.locationDidUpdateToLocation(location: last)
}
}
- 由于它是只有 1 个代表的单例,请确保您没有将代表设置为
nil
或其他短期对象
推荐阅读
- javascript - 类型 [] 打字稿上不存在属性
- django-serializer - Django Serializer 的问题涉及带有 null 和布尔值 true 或 false 的嵌套 JSON
- amazon-web-services - 发布到 SNS 主题的消息在没有订阅时可访问的任何详细信息?
- jq - 试图从 JQ 获得正确的输出
- c++ - 如何使数组的元素为空?
- ruby-on-rails - 通过另一个 rails 应用程序运行 rails 应用程序的 rake 任务
- python - Python:通过列中的项目数复制数据框中的行
- pandas - 获取数据框中的重要特征
- python - How to avoid step into built-in functions when debugging in PyCharm?
- javascript - 如何使用正则表达式格式化带有国家代码的手机号码