在viewdidload()
函数中,我让它运行所有保存的经度和纬度值。然后,它在 map 上标记具有匹配键的 pooplocationsLONG 和 pooplocationsLAT 所在位置。我遇到的问题是它在打印 print ("starting\(i)")
后停止。它遍历所有当前键并且它们都有一个值,但它不执行 if 语句中的内容。
它一直到达 print("starting\(i)")
并一直打印出正确的数字。由于输出 print(pooplocationsLONG)
,我知道它的值不为零。它永远不会到达print("1")
。我使用 if 语句是否错误?
import UIKit
import CoreLocation
import MapKit
class poopstistics: UIViewController,CLLocationManagerDelegate {
@IBOutlet var label: UILabel!
var pooplocationsLONG: [Int: Double] = [0: 0]
var pooplocationsLAT: [Int: Double] = [0: 0]
@IBOutlet var map: MKMapView!
let manager = CLLocationManager()
func recordLocation() {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
Globalvariables.numofdumps += 1
print("a")
let defaults = UserDefaults.standard
print("b")
defaults.set(Globalvariables.numofdumps, forKey: "numofdumpskey")
print("c")
pooplocationsLONG[Globalvariables.numofdumps] = locValue.longitude
pooplocationsLAT[Globalvariables.numofdumps] = locValue.latitude
print("d")
print(pooplocationsLONG)
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
label.text = "Total Dumps: \(Globalvariables.numofdumps)"
for i in 1...Globalvariables.numofdumps {
print("starting\(i)")
if pooplocationsLONG[i] != nil{
print("1")
let annotation = MKPointAnnotation()
print("2")
annotation.coordinate = CLLocationCoordinate2DMake(pooplocationsLONG[i]!,pooplocationsLAT[i]!)
print("3")
annotation.title = "DUMP \(i)"
print("4")
map.addAnnotation(annotation)
print("5")
}
}
}
}
最佳答案
你的 for 是错误的。 0 中的 swift init 中的数组。
for i in 0...Globalvariables.numofdumps - 1 {}
关于ios - 为什么我的 if 语句不执行里面的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47537742/