[英]Is there a way to iterate through elements in an array in Swift, by incrementing the count with a UIButton action?
[英]How to know if array count is incrementing in didUpdateLocations Swift
您好,我是 swift 的新手。 我正在使用 Google Maps 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 有一种叫做属性观察器的东西,你可以用它在设置/更改属性时执行代码。 它们是willSet
和didSet
,它们也适用于数组。 您可以在此处阅读有关属性和属性观察者的更多信息
一个例子
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.