我不明白 属性 'self.heading' not initialized at super.init 我的代码调用错误

I don't understand Property 'self.heading' not initialized at super.init call error in my code

我创建了一个 class 来为我的 SwiftUI 应用程序使用 CLLocationManagerDelegate 获取当前标题,但是我遇到了 Property 'self.heading' not initialized at super.init call 错误。我不知道为什么。我认为我不需要指定参数,因为我的 class 在调用期间不接受任何参数 ?

我的位置管理器class:

class LocationManager: NSObject, CLLocationManagerDelegate, ObservableObject {

  var locationManager: CLLocationManager?
  var objectWillChange = PassthroughSubject<Void, Never>()

  @Published var heading: Double {
    willSet {
      objectWillChange.send()
    }
  }

  override init() {
    super.init()

    locationManager = CLLocationManager()
    locationManager!.delegate = self
    locationManager!.desiredAccuracy = kCLLocationAccuracyBest
    locationManager!.requestWhenInUseAuthorization()
  }

  func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    self.heading = newHeading.trueHeading.degreesToRadians
  }

}

必须初始化当前class的所有实例变量初始化super.

因此在调用 super.init() 之前向 heading 添加一些初始值,例如:

override init() {
    heading = 0 // Or anything you need
    super.init()

    locationManager = CLLocationManager()
    locationManager!.delegate = self
    locationManager!.desiredAccuracy = kCLLocationAccuracyBest
    locationManager!.requestWhenInUseAuthorization()
}