如何知道didUpdateLocations中的数组计数是否在增加



我是Swift的新手。我使用谷歌地图Sdk的方法didUpdateLocations在地图上绘制路径。

我正在写一个关于数组计数的部分。如果数组计数增加,我想运行一些函数。我将lat和long存储在两个数组中。

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)
        
    }

如果数组计数增加,是否有任何运算符可以显示数组内容?

Swift有一种叫做属性观测器的东西,当设置/更改属性时,您可以使用它来执行代码。它们是willSetdidSet,它们对阵列也能很好地工作。你可以在这里阅读更多关于属性和属性观察员

示例

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 = []

打印

阵列大小为1
阵列大小为2
矩阵大小为3
数组大小为0

最新更新