I am new to Swift. I am using Google Maps Sdk's method didUpdateLocations to draw a path on the map.
I am working on a section 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?
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