I’ve a collection of a usertype News currently stored in a NSArray. I have to store them in UserDefaults to save them on the Device. A News have a property ID which is unique. Before storing them to UserDefaults, I convert the NSArray to a NSDictionary where the key is the ID and the object for this key is the News. Is this a good way to store them?
The reason for choosing an NSDictionary is to be able to check if a News with a specific ID exists in the list of News each time I redownload the News from a Web Service. This way I have only one News instance for each News ID. I think this would be hard to manage using a normal NSArray — or I’m missing something?
UserDefaults are for storing preferences, settings and other “small” bit of information, they are not for storing application data. You do not “have” to use NSUserDefaults, there are many other options available to you. Writing to files (in the document directory) in their original form or using NSKeyedArchiver/NSKeyedUnarchiver for example.
You can get the document directory path like this
Apart from that storing an NSDictionary with News ids as keys is fine. I don’t really see the point of having an NSArray and “converting” it to an NSDictionary, use one or the other (that way you always ensure that you only have one News per item).