我试图让用户选择保存他们的当前位置。我使用默认值来保存它,我发现了如何获取用户位置。但我不确定如何将这两项任务结合起来。所以这是我的问题:
如何让我的函数调用用户位置,以便保存?
@IBAction func addLocation(_ sender: Any) {
// CALL LOCATION MANAGER TO GET LOCATION IN LAT AND LONG
self.saveDefaults()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
func saveDefaults()
{
UserDefaults.standard.set(self.pickers, forKey: "pickers")
print("SAVED PICKERS: \(pickers)")
}
Run Code Online (Sandbox Code Playgroud)
这是我最近的尝试,但我不知道为什么我的 struct (lat: ,long:) 没有接受输入
@IBAction func addLocation(_ sender: Any) {
var locations: [CLLocation]
let manager: CLLocationManager
let userLocation:CLLocation = locations[0] as CLLocation
// Call stopUpdatingLocation() …Run Code Online (Sandbox Code Playgroud)