I got a little problem with changing the value of an object. I have got the following code and want to change the value of the object.
import SwiftUI
import MapKit
import CoreLocation
struct ContentView: View {
@State private var address = ""
@State private var object = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
var body: some View {
VStack {
Text("(object)")
TextField("Type address", text: $address)
Button("Change object") {
getLocation(address: address, object: object)
print(object)
}
}
}
func getLocation (address: String, object: CLLocationCoordinate2D) -> CLLocationCoordinate2D{
let address = address
let object = object
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { placemarks, error in
let placemark = placemarks?.first
self.object.latitude = placemark?.location?.coordinate.latitude ?? 0.0
self.object.longitude = placemark?.location?.coordinate.longitude ?? 0.0
}
return object
}
}
The app displays the correct value of the address, but in the “print” command the default data is shown.
How do I change the value=