Kamaleshwaran
Kamaleshwaran

Reputation: 1

How do I save a custom c++ class object to NSUserDefaults?

I want to add a C++ Class object into NSUserDefaults. If I add it directly, it crashes. How do I add c++ Class object to NSUserDefaults?

Upvotes: 0

Views: 465

Answers (1)

Gobra
Gobra

Reputation: 4261

NSUserDefaults supports some NSObject-based classes to be encoded out of the box. Proper way to save custom NSObject classes into NSUserDefaults instance is to encode them into NSData and save it.

In your case, since you have a C++ class, you could either wrap it's data to an NSObject (NSDictionary maybe) or implement a class <-> string serialisation and deserialisation, saving a string object into the NSUserDefaults instance.

Upvotes: 3

Related Questions