ios - 为什么我的 if 语句不执行里面的内容

标签 ios swift

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/

相关文章:

ios - 在 iOS 中滚动 TableView 时保留 Tableview 单元格值

ios - UITextField 的不同过滤器(实例变量)

swift - 我如何在另一个类中使用此扩展委托(delegate)更新我的 ViewController UI

ios - 错误 : Bundle only contains bitcode-marker

ios - UITableViewCell 按钮单击在 TableView 控件的多个部分中触发

ios - 重叠的声音

ios - 发送异步请求 :queue:completionHandler: With Delegate Methods

ios - 我们可以使用一个具有两种不同编程语言的两个不同项目的单个应用程序吗?

ios - 与 Deinit 和内存泄漏混淆

ios - 将 UITableViewCell 类中的选择器添加到 UITableViewController 类