簡體   English   中英

觀察另一個類的屬性

[英]Observe another class property

我有一個單例課程來管理我的應用程序中的位置:

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”,也永遠不會調用此方法。

var currentLocation更改為dynamic var currentLocation 您必須將動態修飾符添加到要在KVO中觀察的任何屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM