I understand how to use NSCoding
to convert my objects to archive objects. That's not my question.
What I'm wondering is why there isn't a default implementation of NSCoding
that could handle probably 99% of cases.
For instance, every time I write a custom class that I want to archive, I perform the following:
- Implement
-(void)encodeWithCoder:
and-(id)initWithCoder:
. - Go down my property list, writing a pair of statements (one encode, one decode) for each property.
- If the property is an object, I use the encode/decodeObject method.
- If the property is a value, I use the corresponding encode/decode method.
- I always use the property's name as my key.
I would suspect that almost every implementation of NSCoding
is exactly like mine, with the only changes being the particular properties that need to be manipulated.
It seems to me that this would be a perfect place for a standard implementation, with the option to override if your particular case if funky.
Do I have a misunderstanding of what's going on? If not, could I add a category on NSObject
to implement this common method on all objects in my projects?