With ObjectCoder, you can encode any Objective-C object on the fly, as long as it conforms to several guidelines. Unlike NSCoding and similar archive methods, ObjectCoder makes use of the dynamic Objective-C runtime, automatically encoding instance variables for you. ObjectCoder has pre-built intelligence on how to encode basic datatypes like int, char, etc. It also comes with some basic custom encoders for root data classes such as NSNumber and NSString. This means that you will already be able to encode most of your objects without additional custom encoding code.
Adding custom encoders is easy, too! Simply create a new category on your custom class called ObjectCoder, and implement the methods objectCoderSerialization
and initWithObjectCoderSerialization:
. See an existing implementation of this category (e.g. NSString+ObjectCoder.m, NSArray+ObjectCoder.m, etc) for guidance.
Encoding your objects with ObjectCoder is very straight forward. ObjectCoder encodes Objective-C objects to primitive NSDictionaries that could be encoding with JSON, KeyedBits, etc. Encode an object like this:
NSDictionary * serialized = [object objectCoderSerialization];
Later, decoding an object can be done in one line as well:
id object = [NSObject objectByDecodingObjectCoderRootObject:serialized];
Because ObjectCoder makes great use of categories, implementing custom coders for custom objects is easy as well, as described above.
ObjectCoder is under no license, and may be used at your own leasure. Thus, if it pleases you to write this code on a piece of paper, fold the paper up and put it in a sandwich for your kid's lunch, I will not be liable for his/her inevoidable health conditions.