怎么才能让Swift持续更新速度

How can I get Swift to continuously update the speed

我一直在学习使用 swift 来制作一个应用程序,并且想制作一个基本的应用程序来告诉你你的速度。但是我不知道如何让它更新速度,目前它只给我初始速度并且从不以当前速度更新标签。这是我到目前为止的代码:

@IBOutlet var speedLabel: UILabel!
@IBOutlet var countLabel: UILabel!

let locationManager = CLLocationManager()
var speed: CLLocationSpeed = CLLocationSpeed()

override func viewDidLoad() {

    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()

    locationManager.startUpdatingLocation()
    speed = locationManager.location!.speed

    if speed < 0 {
        speedLabel.text = "No movement registered"
    }
    else {
        speedLabel.text = "\(speed)"
    }


}

使用委托的方法https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate

func locationManager(_ manager: CLLocationManager, 
       didUpdateLocations locations: [CLLocation]) {

        guard let speed = manager.location?.speed else { return }
        speedLabel.text = speed < 0 ? "No movement registered" : "\(speed)"
}

另外你调用了两次locationManager.startUpdatingLocation(),所以你可以删除一个调用