为什么我的if语句不在

时间:2017-11-28 17:39:57

标签: ios swift

viewdidload()函数中我运行了所有保存的经度和纬度值。然后它在地图上制作了一个pin,其中pooplocationsLONG和pooplocationsLAT具有匹配的键。我遇到的问题是它在打印("starting\(i)")后停止。它遍历所有当前的键,它们有一个值,但它在if语句中没有执行什么。

它一直到print("starting\(i)")并且一直打印正确的数字。我知道由于输出print(pooplocationsLONG),它的值不是nil。它永远不会到达the 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")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你错了。数组在swift init中为0。

for i in 0...Globalvariables.numofdumps - 1 {}