简体   繁体   中英

How to know if array count is incrementing in didUpdateLocations Swift

Hello I am new to swift . I am using Google Maps Sdk's method didUpdateLocations to draw a path on the map . I just need some help regarding the array count ...
I want to run some functions if the array count is increasing . I am storing lat and long in two arrays .

var latarray = [Double]()
var longarray = [Double]()

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

           locationManager.startMonitoringSignificantLocationChanges()
           locationManager.startUpdatingLocation()

        myMapView.clear()


        if (self.latarray.count != 0 ) {
        longarray.append(long)
        latarray.append(lat)
        print ("lat array is \(latarray)count is \(latarray.count)")
        print ("long array is \(longarray)count is \(longarray.count)")
        }
            else {
            Print("array not increasing ")
               }
         let location = locations.last
        self.lat = (location?.coordinate.latitude)!
        self.long = (location?.coordinate.longitude)!


        let currtlocation = CLLocation(latitude: lat, longitude: long)

    }


Is there any operator which can show if the array content if array count is increasing .
Thanks and regards ..

Swift has something called property observers that you can use to execute code when a property is set/changed. They are willSet and didSet and they work fine for arrays as well. You can read more about properties and property observers here

An example

struct Test {
    var array = [Int]() {
        didSet {
            print("Array size is \(array.count)")
        }
    }
}

var test = Test()

test.array.append(1)
test.array.append(1)
test.array.append(1)
test.array = []

prints

Array size is 1
Array size is 2
Array size is 3
Array size is 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM