I like the plist serialization capability for small collections of objects: it is simple, the storage is XML, etc. But I find setting values in dictionaries cumbersome:
[myDict setObject:keepThis forKey:@"ivar"];
I'd much prefer to use object derived of classes (or even just structs):
myObj.ivar = keepThis
... and then send a message to a collection to get a plist. The classes/structs used for this purpose could be restricted so that they map directly and easily to plist primitives; for example myObj could be mapped to a NSDictionary and a requirement could be placed on ivars such that they are one of the plist primitives.
Has someone already built a facility for this, or should I roll my own?
I'm not sure that this is quite what you're talking about, but Key Value Coding will, among many other things, let you get a dictionary with values from an instance's data, and also set instance data from a dictionary.
There's also a way to ask an instance for a list of its attributes, though it turns out that you have to write some code yourself to make that work. (You, could, though, use the runtime introspection to implement that, if you wanted.) EDIT 2 January 2011: -[NSObject attributeKeys] no longer appears in the iOS documentation. I've edited the link to point to the Mac documentation, but be aware that this may only be an option on that platform.
(The NSCoding protocol is another way of letting your objects store/restore themselves, but the storage is binary, not XML, and you are still responsible for getting/putting each value you care about when asked by the system to do so.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With