How to use userDefaults with multiple view controllers?

In my code, a user enters their address, and gets the coordinates of that address in return. These coordinates should get saved to user defaults. Then, these coordinates are accessed by another view, and a mapView with a pin of the user's address is shown.

ViewController 1

    @IBOutlet var geocodeButton: UIButton!

    @IBAction func geocode(_ sender: UIButton) {

        guard let country = countryTextField.text else { return }

        guard let street = streetTextField.text else { return }

        guard let city = cityTextField.text else { return }

        // Create Address String

        let address = "\(country), \(city), \(street)"

        // Geocode Address String

        geocoder.geocodeAddressString(address) { (placemarks, error) in

            // Process Response

            self.processResponse(withPlacemarks: placemarks, error: error)

        }

        // Update View

        geocodeButton.isHidden = true

        activityIndicatorView.startAnimating()

    }

    private func processResponse(withPlacemarks placemarks: [CLPlacemark]?, error: Error?) {

        // Update View

        geocodeButton.isHidden = false

        activityIndicatorView.stopAnimating()

        if let error = error {

            print("Unable to Forward Geocode Address (\(error))")

            locationLabel.text = "Unable to Find Location for Address"

        } else {

            var location: CLLocation?

            if let placemarks = placemarks, placemarks.count > 0 {

                location = placemarks.first?.location

            }

            if let location = location {

                let coordinate = location.coordinate

                locationLabel.text = "\(coordinate.latitude), \(coordinate.longitude)"

                UserDefaults.setValue(coordinate.latitude, forKey: "lat")

                UserDefaults.setValue(coordinate.longitude, forKey: "long")
            } else {

                locationLabel.text = "No Matching Location Found"

            }

        }

    }


extension String {

    var doubleValue: Double {

        return Double(self) ?? 0

    }

}

View Controller 2:

override func viewDidLoad() {

        super.viewDidLoad()

        

        if let latitude = UserDefaults.value(forKey: "lat") as? Double{

            coordinate1.latitude = lat

        }

        if let longitude = UserDefaults.value(forKey: "lat") as? Double{

            coordinate1.longitude = long

        }

When I try to run this, I get an error:

Thread 1: "[<NSUserDefaults 0x1dad81bd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lat."

Please help me solve this issue soon, thanks!

It has nothing to do with multiple ViewControllers.

You need to use UserDefaults.standard. instead of UserDefaults.:

          UserDefaults.standard.setValue(coordinate.latitude, forKey: "lat")
          UserDefaults.standard.setValue(coordinate.longitude, forKey: "long")

You have to work with an instance (standard) of the class UserDefaults, not with the class itself.

That's what the message means:

NSUserDefaults 0x1dad81bd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lat."

NSUserDefaults is not key value coding-compliant. But its instances are.

How to use userDefaults with multiple view controllers?
 
 
Q