Swift class 初始化

Swift init of class

我是 swift 的新手,无法理解以下代码:


import Foundation
import MapKit
import CoreLocation


class SpeedViewModel: UIViewController, ObservableObject, CLLocationManagerDelegate {
    var locationManager: CLLocationManager = CLLocationManager()
    var speedtest = " km/h"

    override func viewDidLoad() {
            super.viewDidLoad()
        print ("test1111")

        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.startUpdatingLocation()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateLocationInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees, speed: CLLocationSpeed, direction: CLLocationDirection) {
        let speedToKPH = (speed * 3.6)
            if (speedToKPH > 0) {
                speedtest = (String(format: "%.0f km/h", speedToKPH))
            } else {
                speedtest = "0 km/h"
            }
    }
}

我不明白为什么这段代码没有启动。尽管它没有任何错误,但似乎从未调用过 viewDidLoad(),因此 class 没有对我的应用程序的其余部分执行任何操作。请帮我找到正确的初始化器。

正如我在评论中所说,而不是 UIViewController 将 class 声明为 NSObject 的子 class 并在标准 init方法。

updateLocationInfo 永远不会被框架调用。您必须实施 locationManager(_ didUpdateLocations:) 并从该位置获取速度。

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

    guard let speed = locations.first?.speed else { return }
    let speedToKPH = speed * 3.6
    if speedToKPH > 0 {
        speedtest = String(format: "%.0f km/h", speedToKPH)
    } else {
        speedtest = "0 km/h"
    }
}

我的完整代码现在看起来像这样:

import Foundation
import MapKit
import CoreLocation


class SpeedViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
    var locationManager: CLLocationManager = CLLocationManager()
    var lastLocation: CLLocation!
    var speedtest = " km/h"
    

    
    override init() {
        super.init()

        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.startUpdatingLocation()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let speed = locations.first?.speed else { return }
        let speedToKPH = speed * 3.6
        if speedToKPH > 0 {
            speedtest = String(format: "%.0f km/h", speedToKPH)
        } else {
            speedtest = "0 km/h"
        }
    }
}

并做它应该做的。谢谢@vadian