0





22

Advertisement

I know we cannot save custom classes to UserDefaults. But it would be very convenient in my code to save a GMSPlace instance to userdefaults and retrieve it.

Ideally, I would like come up with the following functions :

func save(selectedPlace: GMSPlace) {}
func getSelectedPlace() -> GMSPlace? {}

I suppose I should convert it to NSData or transform it into a dictionary. I went for the second option but I don't know how to retrieve a GMSPlace instance out of a dictionary. In other words, for now, I know only how to do this :

func save(selectedPlace: GMSPlace) {}
func getSelectedPlace() -> [String: Any]? {}
Question author Ambroise-collon | Source

Advertisement


0


You can't save GMSPlace in userdefault because of GMSPlace class not confirm to NSEncoding & NSDecoding protocols.

But can achive this by creating a NSDictionary with all parameters from GMSPlace class and save that newly create NSDictionary to NSUserdefaults.

And when you access agian then get the dictinary from NSUserdefault and create the GMSPlace class object.

Let us know if you have any query in implementation.

Answer author Patrick-r