我是 swiftUI 的新手,我正在嘗試處理一個使用 CoreLocation 進行某些地點位置比較的類。但是我已經在我的班級中添加了我的結構化位置陣列,并且我的override init().
我的課 :
import Foundation
import CoreLocation
import Combine
import SwiftUI
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
@ObservedObject var placeLibrary: PlaceLibrary
@Published var locationStatus: CLAuthorizationStatus?
@Published var lastLocation: CLLocation?
@Published var distanceFromNearest: Double = 0.0
@Published var nearestObject:String = ""
override init() {
placeLibrary.testPlace = placeLibrary.testPlace
super.init() // HERE I GET MY ERROR
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
self.placeLibrary = placeLibrary
}
var statusString: String {
guard let status = locationStatus else {
return "unknown"
}
switch status {
case .notDetermined: return "notDetermined"
case .authorizedWhenInUse: return "authorizedWhenInUse"
case .authorizedAlways: return "authorizedAlways"
case .restricted: return "restricted"
case .denied: return "denied"
default: return "unknown"
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
locationStatus = status
print(#function, statusString)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
lastLocation = location
for (idx, readOnlyPlace) in placeLibrary.testPlace.enumerated() {
// Calculate stuff
let currentLocation = CLLocation(latitude: (self.lastLocation?.coordinate.latitude) ?? 0.0, longitude: (self.lastLocation?.coordinate.longitude) ?? 0.0)
let comparedLocation = CLLocation(latitude: readOnlyPlace.lat, longitude: readOnlyPlace.long)
// Update struct
placeLibrary.testPlace[idx].proximity = currentLocation.distance(from: comparedLocation)
}
placeLibrary.testPlace = placeLibrary.testPlace.sorted(by: { $0.proximity < $1.proximity })
print(placeLibrary.testPlace)
}
}
這里的錯誤結果是:Property 'self.placeLibrary' not initialized at super.init call
在互聯網上查看后,我了解到我需要將我的類使用的所有變數定義到 Init 中。這就是為什么我沒有成功地添加這一行:self.placeLibrary = placeLibrary即使在該super.init()行之前或之后......
所以我覺得有些地方我不明白......
我的地方圖書館:
class PlaceLibrary: ObservableObject{
@Published var testPlace = [
Place(lat: 46.1810, long: 6.2304, Name: "Place 1", proximity: 0.0),
Place(lat: 46.1531, long: 6.2951, Name: "Place 2", proximity: 0.0),
Place(lat: 46.1207, long: 6.3302, Name: "Place 3", proximity: 0.0)
]
}
我的地方結構:
struct Place: Identifiable{
let id = UUID().uuidString
var lat: Double
var long: Double
var Name: String
var proximity: Double
init (lat: Double, long: Double, Name: String, proximity: Double){
self.lat = lat
self.long = long
self.Name = Name
self.proximity = proximity
}
init(config: NewPlaceConfig){
self.lat = config.lat
self.long = config.long
self.Name = config.Name
self.proximity = config.proximity
}
}
最后是我的 NewPlaceConfig
struct NewPlaceConfig{
var lat: Double
var long: Double
var Name: String
var proximity: Double
}
uj5u.com熱心網友回復:
正如錯誤訊息所說,該placeLibrary屬性未初始化。
ObservedObject,在這種情況下,什么都不做。屬性包裝器@ObservedObject只能在View結構中使用。它告訴View觀察物件的任何變化,以便 Swift 知道何時重繪或重新計算視圖。除此之外,它在初始化方面沒有任何作用。
所以,真的,該屬性被宣告為var placeLibrary: PlaceLibrary. 此宣告不會初始化屬性,它只描述它將持有的型別。
您可以在super.init呼叫之前自己初始化它 using self.placeLibrary = PlaceLibrary(),或者更簡潔地說,在屬性宣告站點本身初始化它:
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
var placeLibrary = PlaceLibrary()
// ...
// Alternatively, initialize the property in the init()
override init() {
self.placeLibrary = PlaceLibrary()
super.init()
// ...
// Remaining initialization code
}
}
我也會替換var為let,但這是您的選擇。
uj5u.com熱心網友回復:
Property 'self.placeLibrary' not initialized at super.init call
問題不在于您的super.init()呼叫,而是因為您試圖在該類完全初始化之前訪問該類的屬性(如訊息所述)。
編輯:起初這似乎很奇怪。但是你不能保證編譯器呼叫和初始化你的類的順序。您只能確定一個類在init()完成后完全初始化。這就是為什么你不能在 init 中使用屬性,根據定義,只能初始化。
Edit2:最簡單的修復方法是可選。如果您確定此屬性將永遠不會再次變為零,您可以使用!.
編輯3:像這樣的東西:
import Foundation
import CoreLocation
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private var number: Int!
override init() {
super.init()
}
convenience init(test: Int) {
self.init()
self.number = test
}
}
class MyClass {
var location: LocationManager
init(location: LocationManager) {
self.location = location
}
}
注意這里的可選屬性Int!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532200.html
標籤:班级变量迅捷宣言在里面
