观察另一个类属性

时间:2016-01-07 12:16:19

标签: ios swift key-value-observing

我有一个管理我的应用中位置的单例类:

import UIKit
import CoreLocation

class Location: NSObject, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()
    var currentLocation:CLLocationCoordinate2D? {
        didSet {
            self.locationManager.stopUpdatingLocation()
        }
    }

    //////////////////////////////////////////////////////////////////////////////
    class var manager: Location {
        return UserLocation
    }


    //////////////////////////////////////////////////////////////////////////////
    override init () {
        super.init()

        if self.locationManager.respondsToSelector(Selector("requestAlwaysAuthorization")) {
            self.locationManager.requestWhenInUseAuthorization()
        }
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.distanceFilter = 50


    }


    //////////////////////////////////////////////////////////////////////////////
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        if self.currentLocation == nil {
            print("User Location NOT Updated.")
        } else {
            print("did update location")
        }
        self.currentLocation = manager.location!.coordinate

    }


    //////////////////////////////////////////////////////////////////////////////
    func startLocationUpdate() {
        self.locationManager.startUpdatingLocation()
    }


    //////////////////////////////////////////////////////////////////////////////
    func stopLocationUpdate() {
        self.locationManager.stopUpdatingLocation()
    }

}

// Mark: - Singleton Location
//////////////////////////////////////////////////////////////////////////////
let UserLocation = Location()

所以从其他任何课程我都可以打电话:

    let theCurrentLocation = UserLocation.currentLocation

但是可以在另一个类中为此属性添加一些观察者吗?

是否可以通过其他一些聪明的方式通知另一个类,该属性已更改?

我找到了addObserver方法,addObserver(observer: NSObject, forKeyPath: String, options: NSKeyValueObservingOptions, context: UnsafeMutablePointer<Void>),但不确定这可以在此上下文中使用。我在找这样的东西?

class AnotherCLass: NSObject {
    override init() {
        super.init()

        // seudo ->
        UserLocation.addObserver(self, UserLocation.currentLocation, "someAction:")
    }

    func someAction() {
        print("currect location has changed..")
    }

}

修改

所以我看了Key-Value Observing,听起来就像我需要的那样。我按照指南进行了操作,但是当我要观察的属性发生变化时,我没有得到任何通知。所以我像这样添加观察者:

UserLocation.addObserver(self, forKeyPath: "currentLocation", options: .New, context: nil)

观察方法如下:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    print("notification received")
    if keyPath == "currentLocation" {
        print("Current Location received")   
    }
}

但是这个方法永远不会被调用,尽管'currentLocation'被改变了......

1 个答案:

答案 0 :(得分:2)

var currentLocation更改为dynamic var currentLocation。您必须将动态修改器添加到要在KVO中观察的任何属性。

相关问题