[英]How to correctly initialize a computed variable in swift and XCode using MapKit?
所以我試圖將設備的緯度存儲在一個名為 userLatiude 的變量中。 我試圖稍后在代碼中訪問該屬性,但直到運行屬性初始化程序之后才創建該變量。 這會導致代碼出現故障。 我嘗試使用惰性計算變量來解決這個問題,但我仍然遇到錯誤。 當前錯誤是“惰性不能與計算屬性一起使用”。
lazy var userLatitude: CLLocationDegrees {
return (self.locationManager.lastLocation?.coordinate.latitude ?? 0)
}()
任何幫助將不勝感激!
無論如何,計算屬性是惰性的,刪除關鍵字lazy
和尾隨括號對
var userLatitude: CLLocationDegrees {
return self.locationManager.lastLocation?.coordinate.latitude ?? 0
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.