簡體   English   中英

如何使用 MapKit 正確初始化 swift 和 XCode 中的計算變量?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM