Closed ghost closed 8 years ago
You have to make sure that all of the data types you are trying to save to nsuserdefaults are compatible. Types such as strings and arrays are compatible. Y ou can save your own custom objects to default but you need to use keyed archiving which is a protocol of Nscoding.
Take a look at the project I put in the playgrounds repository, it shows an example of saving a custom object using keyed archiving.
Excellent. One other question: I have implemented NSCoding for a custom classes but encapsulated instances cannot be cast to NSData. Do all member instances of a class to be encoded as NSData also need to inherit from NSObject, NSCoding?
That is exactly correct. Every object you want to encode to nscoding must conform to nscoding.
Thanks! My app now has persistent data and can pass an NSData object to another device.
Tried archiving my data singleton to NSURL, received SNAFU.
Is it safe or harmful to store the app's data in NSUserDefaults if all the data is relevant to the app's basic behaviour? I recognize that CoreData or a structured DB is better if the app is generating big files which are only occasionally accessed, but that is not the case.