ios - 如何知道数组计数是否在 didUpdateLocations Swift 中增加

标签 ios swift xcode gmsmapview

您好,我是 swift 新手。我正在使用 Google Maps Sdk 的方法 didUpdateLocations 在 map 上绘制路径。我只需要一些关于数组计数的帮助......
如果数组计数增加,我想运行一些函数。我将 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它们也适用于数组。您可以阅读有关属性和属性观察者的更多信息 here

一个例子

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

打印

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

关于ios - 如何知道数组计数是否在 didUpdateLocations Swift 中增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60558906/

相关文章:

ios - 如何将点击监听器添加到选定的 mkannotationview?

ios - 在代码中设置IBOutlet的自动布局约束

ios - AVAudioEngine AudioFile 时间管理

ios - 在 UITableViewCell 中重新加载 Collectionview Cell

ios - 如何在 MKMapView 的 MKAnnotation 上设置图像

ios - UISplitViewController 作为自定义选项卡栏 Controller 的子项

ios - 需要从我的类中的函数返回数组

xcode - 如何设置 Xcode Organizer 以使用 x86_64 二进制文件运行?

swift - 我怎样才能把我的收藏 View 放在导航栏 Swift 下

ios - 使用 Xcode 6 的 iOS 8 模拟器下的应用程序性能缓慢